Unable to send variable to another form
-
Hi
I’m still new to QT and have just started using slots and signals. I’m trying to send a variable from one form to another, and then the other way back, however I wanted to attempt to do it one way first, however I’m getting an error which no matter what I try it won’t go away. I have two forms MainWindow and Settings my code is below
Mainwindow.h
signals: void SendData(int speed_check_mainform);Settings.h
private slots: void getData(int speed_check_mainform);Settings.cpp
void Settings::getData(int speed_check_mainform){ qDebug() << speed_check_mainform; }MainWindow.cpp
connect(MainWindow,SIGNAL(SendData(int)),Settings,SLOT(getData(int)));in the MainWindow.cpp is where i am getiing the error
expected primary-expression before ',' tokenI’m not sure if it is something simple or not, I have looked at many examples online and I always get the same error from the connect, I have put it in the main() however still nothing. I of course know that I need to emit when using the signal but I just can’t seem to get passed this error. if anyone could help that would be great
Thank you
-
Hi
I’m still new to QT and have just started using slots and signals. I’m trying to send a variable from one form to another, and then the other way back, however I wanted to attempt to do it one way first, however I’m getting an error which no matter what I try it won’t go away. I have two forms MainWindow and Settings my code is below
Mainwindow.h
signals: void SendData(int speed_check_mainform);Settings.h
private slots: void getData(int speed_check_mainform);Settings.cpp
void Settings::getData(int speed_check_mainform){ qDebug() << speed_check_mainform; }MainWindow.cpp
connect(MainWindow,SIGNAL(SendData(int)),Settings,SLOT(getData(int)));in the MainWindow.cpp is where i am getiing the error
expected primary-expression before ',' tokenI’m not sure if it is something simple or not, I have looked at many examples online and I always get the same error from the connect, I have put it in the main() however still nothing. I of course know that I need to emit when using the signal but I just can’t seem to get passed this error. if anyone could help that would be great
Thank you
@VJ01 said in Unable to send variable to another form:
connect(MainWindow,SIGNAL(SendData(int)),Settings,SLOT(getData(int)));At a guess, from your
Settings.cppextractvoid Settings::getData()it looks likeSettingsis a class name. For signal/slot you must connect instances (presumablyMainWindowis one?), what is yourSettingsinstance's variable?Next, given you have declared the
getData()slot asprivateinSettings.hobviously you are not going to be able to access it on aSettingsinstance anyway, only from withinSettingsclass. If you want toconnect()to it from the outside world/on an instance don't make itprivate, that is whatprivateis there to prevent!You should not be using
SIGNAL/SLOT()macros forconnect(), they are old-style from many years ago. You should be using new style, the error messages may be more helpful. -
@VJ01 said in Unable to send variable to another form:
connect(MainWindow,SIGNAL(SendData(int)),Settings,SLOT(getData(int)));At a guess, from your
Settings.cppextractvoid Settings::getData()it looks likeSettingsis a class name. For signal/slot you must connect instances (presumablyMainWindowis one?), what is yourSettingsinstance's variable?Next, given you have declared the
getData()slot asprivateinSettings.hobviously you are not going to be able to access it on aSettingsinstance anyway, only from withinSettingsclass. If you want toconnect()to it from the outside world/on an instance don't make itprivate, that is whatprivateis there to prevent!You should not be using
SIGNAL/SLOT()macros forconnect(), they are old-style from many years ago. You should be using new style, the error messages may be more helpful.@JonB hi thank you for the reply i tried to implement it a different way
MainWindow.cpp
void MainWindow::on_actionSettings_triggered() { qDebug() << "Settings"; Settings *dlg = new Settings(); connect(this, SIGNAL(sendData(int)), dlg, SLOT(setData(int))); emit sendData(35); settings.show(); }Settings.cpp
void Settings::setData(int data) { printf("BLAH BLAH BLAH %d\n", data); ui->lineEdit->setText(data); }my printf function does show that data is 35 however i cant seem to get the line Edit to work
Thank you
-
@JonB hi thank you for the reply i tried to implement it a different way
MainWindow.cpp
void MainWindow::on_actionSettings_triggered() { qDebug() << "Settings"; Settings *dlg = new Settings(); connect(this, SIGNAL(sendData(int)), dlg, SLOT(setData(int))); emit sendData(35); settings.show(); }Settings.cpp
void Settings::setData(int data) { printf("BLAH BLAH BLAH %d\n", data); ui->lineEdit->setText(data); }my printf function does show that data is 35 however i cant seem to get the line Edit to work
Thank you
@VJ01 said in Unable to send variable to another form:
however i cant seem to get the line Edit to work
Because setText expects a QString as you can clearly see in the documentation: https://doc.qt.io/qt-6/qlineedit.html#text-prop
So, convert the int to QString: https://doc.qt.io/qt-6/qstring.html#number-1 -
@VJ01 said in Unable to send variable to another form:
however i cant seem to get the line Edit to work
Because setText expects a QString as you can clearly see in the documentation: https://doc.qt.io/qt-6/qlineedit.html#text-prop
So, convert the int to QString: https://doc.qt.io/qt-6/qstring.html#number-1sorry about that i pasted the wrong code
void Settings::setData(int data) { printf("BLAH BLAH BLAH %d\n", data); ui->lineEdit->setText(QString::number(data)); }this is the code im using but has the same effect and nothing happens
never mind i was just being really thick i should have done this in my MainWindow.cppdlg->show();