a Problem with Signal/Slot
-
Hello, I am new to Qt ( not to C++ at least ) and I have a problem with making the following work:
I defined a class that holds two integers with setters/getters in a header then declared a class instance in mainwindow.cpp .
I want to get the user's input (twoint
s) after clicking on a button from aQDialog
(it has it's own subclass in a seperate .cpp and .h namely dialogchangesev.h and dialogchangesev.cpp) that holds twoQLineEdit
, then set the properties of the class instance in mainwindow.cpp to the two inputtedint
.
The problem is : I get a set of errors. ( as shown below )
Thanks.Code fragments:
// dialogchangesev.cpp DialogChangeSEV::DialogChangeSEV(QWidget *parent) : QDialog(parent), ui(new Ui::DialogChangeSEV) { ui->setupUi(this); connect(DialogChangeSEV, SIGNAL( sendIntData(int, int) ), MainWindow, SLOT( setIntData(int,int))); // Error : C2275: 'DialogChangeSEV' : illegal use of this type as an expression // Error: C2275: 'MainWindow' : illegal use of this type as an expression // see declaration of 'MainWindow' } // code goes here ... void DialogChangeSEV::on_setSEV_clicked() { int se, sv; se = ui->setSE->text().toInt(); sv = ui->setSV->text().toInt(); // emit sendIntData(se,sv) ; }
//dialogchangesev.h // code goes here ... signals: void sendIntData(int datae, int datav); };
//mainwindow.h // code goes here ... public slots: void setIntData(int datae,int datav);
//mainwindow.cpp // code goes here ... includes and so on sizeEV gDimensions; // ... void MainWindow::setIntData(int datae,int datav){ gDimensions.setSE(datae); gDimensions.setSV(datav); }
-
@mHeidelber said:
connect(DialogChangeSEV, SIGNAL( sendIntData(int, int) ),
MainWindow, SLOT( setIntData(int,int)));
// Error : C2275: 'DialogChangeSEV' : illegal use of this type as an expression
// Error: C2275: 'MainWindow' : illegal use of this type as an expressionThe MS compiler is correct! You can't connect classes, you can connect objects, so this should be:
connect(this, SIGNAL(sendIntData(int, int)), parent, SLOT(setIntData(int,int)));
assuming the parent is the
MainWindow
.But you should really make the connection from outside the dialog, not from its constructor, otherwise you impose on the dialog to know who's his parent/receiver, which defeats the whole purpose of the signal-slot mechanism.
Kind regards.
-
HI,
Have a glance of using signal & slots.
http://doc.qt.io/qt-4.8/signalsandslots.html