QPushbutton click event not working
-
wrote on 16 May 2022, 21:13 last edited by progloveprog
Hi, I have watched this page ( https://wiki.qt.io/Qt_for_Beginners#Responding_to_an_event) a lot of time to understand QT
Unfortunately, I have issue with click event on QPushbutton and I do not understand how can I fix that.
This is the code what I have now.
main.cpp
#include <QApplication> #include <QPushButton> #include <QLineEdit> #include <QMessageBox> //#include <QIcon> int main(int argc, char **argv) { QApplication app (argc, argv); QWidget window; window.setFixedSize(400,400); window.setWindowTitle("sign up application"); QLineEdit *usernameLine = new QLineEdit(&window); usernameLine->setGeometry(100,50,200,30); usernameLine->setPlaceholderText("username"); QLineEdit *passwordLine = new QLineEdit(&window); passwordLine->setPlaceholderText("password"); passwordLine->setGeometry(100,90,200,30); QLineEdit *passwordRepeatLine = new QLineEdit(&window); passwordRepeatLine->setPlaceholderText("password repeat"); passwordRepeatLine->setGeometry(100,130,200,30); QPushButton *button = new QPushButton("Submit",&window); button->setGeometry(150,180,100,30); QObject::connect(button, SIGNAL(clicked()),this, SLOT(signUp())); window.show(); return app.exec(); } void signUp(){ QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.exec(); }
Problem is in QObject::connect. QT creator tells me
invalid use of this in non-member function so I guess, in this circumstances I cannot use this. So as the tutorial says, I changed this into QApplication::instance() like this.connect(this, SIGNAL (counterReached()), QApplication::instance(), SLOT (signUp()));
But in this case, QT shows me
No such slotQObject::connect: No s QApplication::signUp()If I modify signUp function like this
void QApplication::signUp(){ QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.exec(); }
QT shows me
error: no 'void QApplication::signUp()' member function declared in class 'QApplication'
void QApplication::signUp(){
^What am I doing wrong??
-
Hi, I have watched this page ( https://wiki.qt.io/Qt_for_Beginners#Responding_to_an_event) a lot of time to understand QT
Unfortunately, I have issue with click event on QPushbutton and I do not understand how can I fix that.
This is the code what I have now.
main.cpp
#include <QApplication> #include <QPushButton> #include <QLineEdit> #include <QMessageBox> //#include <QIcon> int main(int argc, char **argv) { QApplication app (argc, argv); QWidget window; window.setFixedSize(400,400); window.setWindowTitle("sign up application"); QLineEdit *usernameLine = new QLineEdit(&window); usernameLine->setGeometry(100,50,200,30); usernameLine->setPlaceholderText("username"); QLineEdit *passwordLine = new QLineEdit(&window); passwordLine->setPlaceholderText("password"); passwordLine->setGeometry(100,90,200,30); QLineEdit *passwordRepeatLine = new QLineEdit(&window); passwordRepeatLine->setPlaceholderText("password repeat"); passwordRepeatLine->setGeometry(100,130,200,30); QPushButton *button = new QPushButton("Submit",&window); button->setGeometry(150,180,100,30); QObject::connect(button, SIGNAL(clicked()),this, SLOT(signUp())); window.show(); return app.exec(); } void signUp(){ QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.exec(); }
Problem is in QObject::connect. QT creator tells me
invalid use of this in non-member function so I guess, in this circumstances I cannot use this. So as the tutorial says, I changed this into QApplication::instance() like this.connect(this, SIGNAL (counterReached()), QApplication::instance(), SLOT (signUp()));
But in this case, QT shows me
No such slotQObject::connect: No s QApplication::signUp()If I modify signUp function like this
void QApplication::signUp(){ QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.exec(); }
QT shows me
error: no 'void QApplication::signUp()' member function declared in class 'QApplication'
void QApplication::signUp(){
^What am I doing wrong??
wrote on 16 May 2022, 21:29 last edited by@progloveprog
Hello and welcome.To start adding slots to your Ui you need to begin by subclassing the
QWidget
you use for yourwindow
. Then you can add thesignUp()
as a method of the subclass and use that for the slot in yourconnect()
.The link you are using notes
Remark : This tutorial series target mainly Qt4. Even if most of these tutorials are also valid for Qt5
Its use of
SIGNAL()
andSLOT()
are pretty old. For all code you write now you should use New Signal Slot Syntax.When pasting code here please enclose using the Code tag, the
</>
icon on the toolbar when you compose posts.Good luck :)
-
@progloveprog
Hello and welcome.To start adding slots to your Ui you need to begin by subclassing the
QWidget
you use for yourwindow
. Then you can add thesignUp()
as a method of the subclass and use that for the slot in yourconnect()
.The link you are using notes
Remark : This tutorial series target mainly Qt4. Even if most of these tutorials are also valid for Qt5
Its use of
SIGNAL()
andSLOT()
are pretty old. For all code you write now you should use New Signal Slot Syntax.When pasting code here please enclose using the Code tag, the
</>
icon on the toolbar when you compose posts.Good luck :)
wrote on 16 May 2022, 22:05 last edited by@JonB Thank you, but I have some questions before I start implement your solution in my code.
Final goal of the program is, when user clicks a submit button, program will read username, password and do some validation like does username have any non-alphabet, are password same.. Do I have to create QWidget on all elements and put them on separate cpp file? or does it work making 1 QWidget on window and create UI elements there and integrate with them?And, I did not know that I can enclose code with code tag. Thank you for info
-
@JonB Thank you, but I have some questions before I start implement your solution in my code.
Final goal of the program is, when user clicks a submit button, program will read username, password and do some validation like does username have any non-alphabet, are password same.. Do I have to create QWidget on all elements and put them on separate cpp file? or does it work making 1 QWidget on window and create UI elements there and integrate with them?And, I did not know that I can enclose code with code tag. Thank you for info
@progloveprog said in QPushbutton click event not working:
Do I have to create QWidget on all elements and put them on separate cpp file?
You can create one widget in its own header/cpp file and put any number of other widgets on it.
-
Hi, I have watched this page ( https://wiki.qt.io/Qt_for_Beginners#Responding_to_an_event) a lot of time to understand QT
Unfortunately, I have issue with click event on QPushbutton and I do not understand how can I fix that.
This is the code what I have now.
main.cpp
#include <QApplication> #include <QPushButton> #include <QLineEdit> #include <QMessageBox> //#include <QIcon> int main(int argc, char **argv) { QApplication app (argc, argv); QWidget window; window.setFixedSize(400,400); window.setWindowTitle("sign up application"); QLineEdit *usernameLine = new QLineEdit(&window); usernameLine->setGeometry(100,50,200,30); usernameLine->setPlaceholderText("username"); QLineEdit *passwordLine = new QLineEdit(&window); passwordLine->setPlaceholderText("password"); passwordLine->setGeometry(100,90,200,30); QLineEdit *passwordRepeatLine = new QLineEdit(&window); passwordRepeatLine->setPlaceholderText("password repeat"); passwordRepeatLine->setGeometry(100,130,200,30); QPushButton *button = new QPushButton("Submit",&window); button->setGeometry(150,180,100,30); QObject::connect(button, SIGNAL(clicked()),this, SLOT(signUp())); window.show(); return app.exec(); } void signUp(){ QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.exec(); }
Problem is in QObject::connect. QT creator tells me
invalid use of this in non-member function so I guess, in this circumstances I cannot use this. So as the tutorial says, I changed this into QApplication::instance() like this.connect(this, SIGNAL (counterReached()), QApplication::instance(), SLOT (signUp()));
But in this case, QT shows me
No such slotQObject::connect: No s QApplication::signUp()If I modify signUp function like this
void QApplication::signUp(){ QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.exec(); }
QT shows me
error: no 'void QApplication::signUp()' member function declared in class 'QApplication'
void QApplication::signUp(){
^What am I doing wrong??
@progloveprog said in QPushbutton click event not working:
QObject::connect(button, SIGNAL(clicked()),this, SLOT(signUp()));
even so the others comments are correct here is what you'll have to change, to make this connect work:
QObject::connect(button, &QPushButton::clicked,&signUp);
also make sure the signUp function is "above" the main() or it won't work either
-
Hi, I have watched this page ( https://wiki.qt.io/Qt_for_Beginners#Responding_to_an_event) a lot of time to understand QT
Unfortunately, I have issue with click event on QPushbutton and I do not understand how can I fix that.
This is the code what I have now.
main.cpp
#include <QApplication> #include <QPushButton> #include <QLineEdit> #include <QMessageBox> //#include <QIcon> int main(int argc, char **argv) { QApplication app (argc, argv); QWidget window; window.setFixedSize(400,400); window.setWindowTitle("sign up application"); QLineEdit *usernameLine = new QLineEdit(&window); usernameLine->setGeometry(100,50,200,30); usernameLine->setPlaceholderText("username"); QLineEdit *passwordLine = new QLineEdit(&window); passwordLine->setPlaceholderText("password"); passwordLine->setGeometry(100,90,200,30); QLineEdit *passwordRepeatLine = new QLineEdit(&window); passwordRepeatLine->setPlaceholderText("password repeat"); passwordRepeatLine->setGeometry(100,130,200,30); QPushButton *button = new QPushButton("Submit",&window); button->setGeometry(150,180,100,30); QObject::connect(button, SIGNAL(clicked()),this, SLOT(signUp())); window.show(); return app.exec(); } void signUp(){ QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.exec(); }
Problem is in QObject::connect. QT creator tells me
invalid use of this in non-member function so I guess, in this circumstances I cannot use this. So as the tutorial says, I changed this into QApplication::instance() like this.connect(this, SIGNAL (counterReached()), QApplication::instance(), SLOT (signUp()));
But in this case, QT shows me
No such slotQObject::connect: No s QApplication::signUp()If I modify signUp function like this
void QApplication::signUp(){ QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.exec(); }
QT shows me
error: no 'void QApplication::signUp()' member function declared in class 'QApplication'
void QApplication::signUp(){
^What am I doing wrong??
wrote on 18 May 2022, 07:21 last edited by@progloveprog said in QPushbutton click event not working:
QObject::connect(button, SIGNAL(clicked()),this, SLOT(signUp()));
In
main()
there is nothis
pointer! @J-Hilk gives you the quick fix to this line. And @JonB and @jsulm explain why and how you should implement your own class instead.BTW, in the link to the beginner's guide you provided it also explains this approach. Forget about the first couple examples on that page as this is not how you cleanly write Qt applications. Rather have a look starting from here: https://wiki.qt.io/Qt_for_Beginners#Subclassing_QWidget
1/6