Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPushbutton click event not working
Forum Update on Monday, May 27th 2025

QPushbutton click event not working

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    progloveprog
    wrote on 16 May 2022, 21:13 last edited by progloveprog
    #1

    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??

    J J S 3 Replies Last reply 16 May 2022, 21:29
    0
    • P progloveprog
      16 May 2022, 21:13

      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??

      J Offline
      J Offline
      JonB
      wrote on 16 May 2022, 21:29 last edited by
      #2

      @progloveprog
      Hello and welcome.

      To start adding slots to your Ui you need to begin by subclassing the QWidget you use for your window. Then you can add the signUp() as a method of the subclass and use that for the slot in your connect().

      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() and SLOT() 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 :)

      P 1 Reply Last reply 16 May 2022, 22:05
      0
      • J JonB
        16 May 2022, 21:29

        @progloveprog
        Hello and welcome.

        To start adding slots to your Ui you need to begin by subclassing the QWidget you use for your window. Then you can add the signUp() as a method of the subclass and use that for the slot in your connect().

        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() and SLOT() 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 :)

        P Offline
        P Offline
        progloveprog
        wrote on 16 May 2022, 22:05 last edited by
        #3

        @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

        jsulmJ 1 Reply Last reply 17 May 2022, 05:22
        0
        • P progloveprog
          16 May 2022, 22:05

          @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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 17 May 2022, 05:22 last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P progloveprog
            16 May 2022, 21:13

            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??

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 17 May 2022, 07:42 last edited by
            #5

            @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


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • P progloveprog
              16 May 2022, 21:13

              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??

              S Offline
              S Offline
              SimonSchroeder
              wrote on 18 May 2022, 07:21 last edited by
              #6

              @progloveprog said in QPushbutton click event not working:

              QObject::connect(button, SIGNAL(clicked()),this, SLOT(signUp()));

              In main() there is no this 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 Reply Last reply
              0

              1/6

              16 May 2022, 21:13

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved