Qt value from lineEdit and isChecked from QCheckBox not working as expected
-
I'm trying to make a QMessageBox pop up when i press a submit button, which works fine, however the message inside is dependent based on whether some checkboxes are checked, and on the value of the lineEdit, as such:
QApplication a(argc, argv); QMainWindow *w = new QMainWindow(); . . . QPushButton but_submit("Submit"); QMessageBox msg_submit; // The following will be so that we can get the val of the GPA and then add it // To the full message that will contain the info of person QString submit_string = "Hello, here's the summary: \n"; submit_string += "Here\'s your value: " + line_misc[0]->text() + ".\n"; if (chk_art->isChecked()) submit_string += "Art major!"; msg_submit.setText(submit_string); . . . QObject::connect(&but_submit, SIGNAL(clicked()), &msg_submit, SLOT(exec())); w->show(); return a.exec();
Everything that is not in the code here i have defined or initialized, and i don't have any warnings or errors when i run the code, all is well, everything is displayed, but it's like nothing is connected...
The message box appears but the message is "Hello, here's the summary: Here's your value: ." When i'm really expecting to see a number, or the art major comment as well if i have the checkbox checked, but i do not, unfortunately.
I've gone through the docs and tried variations, such as using a spinBox and slider, and using the property function value() to just get the value and wrapping it in QString::number(), as such:
QSlider *slider = new QSlider(Qt::Horizontal); QSpinBox *spinBox = new QSpinBox; spinBox->setRange(0, 130); slider->setRange(0, 130); QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int))); QObject::connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int))); spinBox->setValue(35); lay_grades->addWidget(spinBox); lay_grades->addWidget(slider);
but no matter what i change the value to, it's like setValue property isn't called when I change the value, or the slot valueChanged() doesn't run either.
Am i doing something wrong?
Thanks for your time
EDIT: Here is a link to the code http://pasted.co/c941c495
-
@Nano-95 said in Qt value from lineEdit and isChecked from QCheckBox not working as expected:
line_misc
where / how is this object filled/set? The same goes for chk_art ...
-
@Christian-Ehrlicher Hey, sorry it's just a lengthy .cpp, so i did not want to put all of the code
The line_misc[miscRows] array is initialized in a for loop
where miscRows is: enum { miscRows = 4 };
and I have some checkboxes for different hobbies, chk_art being one of them:
for (int i = 0; i < miscRows; ++i) { line_misc[i] = new QLineEdit; lay_grades->addWidget(line_misc[i]); } QCheckBox *chk_art = new QCheckBox(); chk_art->setText("Art");
These are all in a layout already added as widgets, and are visual when i run the code
-
@Nano-95 without seeing the whole source code, I suspect that you need an interim step from the moment submit button is clicked until message box is display to build the message with all the current values. Pseudo-code:
QObject::connect(&but_submit, SIGNAL(clicked()), &this, SLOT(displayMBox())); ... void displayMBox() { QString submit_string = "Hello, here's the summary: \n"; submit_string += "Here\'s your value: " + line_misc[0]->text() + ".\n"; if (chk_art->isChecked()) submit_string += "Art major!"; msg_submit.setText(submit_string); msg_submit.exec() }
-
@Pablo-J.-Rogina
Thanks for the response, I'm assuming that the use of &this would work inside a class... But i get errors due to the fact that this is all inside the main.cpp?
Here's the code: http://pasted.co/c941c495I got some errors, so i fixed it up a bit, nothing happened
Wit the code in the link, any ideas?
Thanks so much for your time!
-
Hi
One error is that you use "this" in main.cpp.
There is no this there so that cannot work.
You should always check the return of connect to see if it did work.
connect return true /false.So this line
QObject::connect(&but_submit, SIGNAL(clicked()), &this, SLOT(displayMBox()));
says "this" for main.cpp and that wont work.you can however use lambda with the new syntax. Note the & to have access to the widgets.
QObject::connect(&but_submit, &QPushButton::clicked, [&]() { QString submit_string = "Hello, here's the summary: \n"; submit_string += "Here\'s your value: " + line_misc[0]->text() + ".\n"; if (chk_art->isChecked()) { submit_string += "Art major!"; } msg_submit.setText(submit_string); msg_submit.exec(); }); //void displayMBox() code is move inside the lambda
I can see you tried the lamda before so not sure why you was not happy with that ?
-
@mrjj
First, thanks for your response,I tried it, yes, \ but it never worked :L I've tried variations to stop receiving errors but I get this with this code
QObject::connect(&but_submit, SIGNAL(clicked()), [&]() { QString submit_string = "Hello, here's the summary: \n"; submit_string += "Here\'s your value: " + line_misc[0]->text() + ".\n"; if (chk_art->isChecked()) { submit_string += "Art major!"; } msg_submit.setText(submit_string); msg_submit.exec(); });
"no matching function for call to 'QObject::connect(QPushButton*, const char*, main(int, char**)::<lambda()>)'"
with what may be a relevant warning "warning: lambda expressions only available with -std=c++0x or -std=gnu++0x [enabled by default]"When i try your code, I get a different error which is why i tried switching it up
/usr/local/Trolltech/Qt-4.8.5/include/QtGui/qabstractbutton.h:127: error: 'void QAbstractButton::clicked(bool)' is protected
-
Hi
You are mixing old syntax with lambda which it cannot do.
With lambdas you cannot use the SIGNAL() macro.
Has to be the new syntax.I used
QObject::connect(&but_submit, &QPushButton::clicked, [&]() {...
which compiled fine and did show the entered string.
But you seems to be using
Qt-4.8.5 at and new syntax first came in Qt5
so i guess that is why you get errors.So basically, with that old Qt , you cannot do it in main.
You need a QOBJECT based object to send signals to.
In that old Qt, its not possible to connect to functions.
Only to member slots in a class.You must use that so old Qt ?