Calling functions between classes
-
Use signal and slot (basic Qt programming)
Emit a signal from your dialog that will be received in a slot of your MainWindow. -
I can see how that will work, but how do I call the MainLogEdit function in MainWindow with emit() from Dialog.
@dencla said in Calling functions between classes:
but how do I call the MainLogEdit function in MainWindow with emit() from Dialog.
You don't call anything, you emit a signal.
Have you defined a signal in Dialog.h
and a slot in MainWindow.hShow us your code.
-
@dencla said in Calling functions between classes:
Yes it is at the top of the post.
No, it is not.
Again: add a signal to your dialog, add a slot to MainWindow, connect both, in your dialog you emit this signal. -
@dencla
@jsulm referred you earlier to https://doc.qt.io/qt-5/signalsandslots.html doc page. There is a heading there A Small Example:A minimal C++ class declaration might read:
That example shows the components you need for @mpergand's
Emit a signal from your dialog that will be received in a slot of your MainWindow.
- Declaration of a signal method. (
Dialog.h
) emit
of the signal. (Dialog.cpp
)- Declaration of a slot method. (
MainWindow.h
) connect()
of the signal-object+signal-method to the slot-object+slot-method. (MainWindow.cpp
)
That is what you need to write. The only difference for you is where in the simple example has both signal & slot in the same class, you will have them in separate classes. 1 & 2 above are to be in your
Dialog
class, 3 & 4 inMainWindow
class. - Declaration of a signal method. (
-
@dencla
@jsulm referred you earlier to https://doc.qt.io/qt-5/signalsandslots.html doc page. There is a heading there A Small Example:A minimal C++ class declaration might read:
That example shows the components you need for @mpergand's
Emit a signal from your dialog that will be received in a slot of your MainWindow.
- Declaration of a signal method. (
Dialog.h
) emit
of the signal. (Dialog.cpp
)- Declaration of a slot method. (
MainWindow.h
) connect()
of the signal-object+signal-method to the slot-object+slot-method. (MainWindow.cpp
)
That is what you need to write. The only difference for you is where in the simple example has both signal & slot in the same class, you will have them in separate classes. 1 & 2 above are to be in your
Dialog
class, 3 & 4 inMainWindow
class.@JonB That is what I am working on I have been watching videos and such to do what you are suggesting. I understand the concept, but I'm sure that it is the syntax that is throwing me. Thanks to all. SOmetimes it takes a lot to get a new idea through to an old brain.
- Declaration of a signal method. (
-
I am understanding the issue that I am having thanks to everyone's help. I am still having an issue with the dialog pushbutton emiti correctly to the Main Window. Below is the code in the MainWindow.cpp the signals and slots are in their prespective header files. The code compiles but does not generate a message in MainWindow textEdit window from the onDataChange signal, but it does for the onNewData signal. I am close but still cannot get the window to update.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
Dialog test;ui->setupUi(this); ui->textBrowser->append("Welcome"); QString text = "Comm Test"; connect(this,&MainWindow::onNewData, this,&MainWindow::MainLogEdit); connect(&test,&Dialog::onDataChange, this,&MainWindow::MainLogEdit);
}
MainWindow::~MainWindow()
{
delete ui;
}int counter;
void MainWindow::MainLogEdit(QString message)
{
ui->textBrowser->append(message);
ui->lineEdit->setText(QString::number(counter++));}
void MainWindow::on_pushButton_clicked()
{
QString message = "This Is a Test!!! ";
emit onNewData(message);
Dialog sdialog;
sdialog.setModal(true);sdialog.exec();
}
I will continue exploring what is needed. It is my guess that the Diaglog test is another instance of Dialog.
Thanks again. -
Hi,
At no point are you connecting the dialog to your main window.
-
So you also updated the connections ?
Can you now properly communicated between the dialog and the main window ? -
Good, then please mark the thread as solved so other forum users may know a solution has been found :-)