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. Connect a function to a slot (action)

Connect a function to a slot (action)

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 664 Views 2 Watching
  • 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.
  • cerrC Offline
    cerrC Offline
    cerr
    wrote on last edited by
    #1

    Hi,

    First off: Qt newbie here, excuse the simple question:
    How can I hook trigger a simple function on the click of a button?
    I've added the following to my ui_main.h header file:

    #include <QtWidgets/QMessageBox>
    class Ui_MainWindow
    public:
    ...
    ...
    void setupUi((QMainWindow *MainWindow)
    {
    ...
    ...
    QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1("bar!")));
    }
    ..
    ...
    public slots:
     void foo1(QString msg) {QMessageBox msgBox;msgBox.setText(msg);msgBox.exec();}
    };
    

    and upon click on the checkallPushButton, I would expect a dialog box to pup up with the string bar! but this isn't happening and I'm wondering why...

    Some context: I'm trying to re-use an existing ui (from someone else) but have to first learn how things exactly connect with each other and the above, is what I thought I've read on various pages on the web....
    Thanks for any help/hints to get this working the way I imagine ;)

    aha_1980A jsulmJ 2 Replies Last reply
    0
    • cerrC cerr

      Hi,

      First off: Qt newbie here, excuse the simple question:
      How can I hook trigger a simple function on the click of a button?
      I've added the following to my ui_main.h header file:

      #include <QtWidgets/QMessageBox>
      class Ui_MainWindow
      public:
      ...
      ...
      void setupUi((QMainWindow *MainWindow)
      {
      ...
      ...
      QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1("bar!")));
      }
      ..
      ...
      public slots:
       void foo1(QString msg) {QMessageBox msgBox;msgBox.setText(msg);msgBox.exec();}
      };
      

      and upon click on the checkallPushButton, I would expect a dialog box to pup up with the string bar! but this isn't happening and I'm wondering why...

      Some context: I'm trying to re-use an existing ui (from someone else) but have to first learn how things exactly connect with each other and the above, is what I thought I've read on various pages on the web....
      Thanks for any help/hints to get this working the way I imagine ;)

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by aha_1980
      #2

      Hi @cerr,

      If you have Qt5 and C++11 compiler, using a lambda would be the simplest:

      QObject::connect(checkallPushButton, &QPushButton::clicked, [] {
          QMessageBox msgBox;msgBox.setText("bar!");msgBox.exec();
      });
      

      (I have not tried your actual message box calls, but it should world I think.

      Regards

      Qt has to stay free or it will die.

      cerrC 1 Reply Last reply
      4
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        Just a few notes.
        The connect is not valid. It should include the types of the parameters, not an actual parameter.
        like
        QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1(QString )));

        However, since clicked() signal do not have a QString as the parameter, they are not compatible.

        That kind of syntax errors can be caught using the new syntax
        https://wiki.qt.io/New_Signal_Slot_Syntax

        or simply checking the return type of connect

        bool res= QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1("bar!")));
        if (!res)
        qDebug() << "connect didnt work"

        1 Reply Last reply
        4
        • aha_1980A aha_1980

          Hi @cerr,

          If you have Qt5 and C++11 compiler, using a lambda would be the simplest:

          QObject::connect(checkallPushButton, &QPushButton::clicked, [] {
              QMessageBox msgBox;msgBox.setText("bar!");msgBox.exec();
          });
          

          (I have not tried your actual message box calls, but it should world I think.

          Regards

          cerrC Offline
          cerrC Offline
          cerr
          wrote on last edited by
          #4

          @aha_1980 said in Connect a function to a slot (action):

          Hi @cerr,

          If you have Qt5 and C++11 compiler, using a lambda would be the simplest:

          QObject::connect(checkallPushButton, &QPushButton::clicked, [] {
              QMessageBox msgBox;msgBox.setText("bar!");msgBox.exec();
          }
          

          (I have not tried your actual message box calls, but it should world I think.

          Regards

          Hi
          Just a few notes.
          The connect is not valid. It should include the types of the parameters, not an actual parameter.
          like
          QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1(QString )));

          However, since clicked() signal do not have a QString as the parameter, they are not compatible.

          That kind of syntax errors can be caught using the new syntax
          https://wiki.qt.io/New_Signal_Slot_Syntax

          or simply checking the return type of connect

          bool res= QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1("bar!")));
          if (!res)
          qDebug() << "connect didnt work"

          Thanks to both, @mrjj and @aha_1980 , both your answers did help me to get something going that will bring me further towards my end goal with this project! Your help is appreciated! Thank you!
          a small addition for whoever else will find this thread:
          @aha_1980 's solutions is missing ); at the end, i.e. the complete lambda style solution looks like:

          QObject::connect(checkallPushButton, &QPushButton::clicked, [] {
              QMessageBox msgBox;msgBox.setText("bar!");msgBox.exec();
          });
          
          aha_1980A 1 Reply Last reply
          4
          • cerrC cerr

            @aha_1980 said in Connect a function to a slot (action):

            Hi @cerr,

            If you have Qt5 and C++11 compiler, using a lambda would be the simplest:

            QObject::connect(checkallPushButton, &QPushButton::clicked, [] {
                QMessageBox msgBox;msgBox.setText("bar!");msgBox.exec();
            }
            

            (I have not tried your actual message box calls, but it should world I think.

            Regards

            Hi
            Just a few notes.
            The connect is not valid. It should include the types of the parameters, not an actual parameter.
            like
            QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1(QString )));

            However, since clicked() signal do not have a QString as the parameter, they are not compatible.

            That kind of syntax errors can be caught using the new syntax
            https://wiki.qt.io/New_Signal_Slot_Syntax

            or simply checking the return type of connect

            bool res= QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1("bar!")));
            if (!res)
            qDebug() << "connect didnt work"

            Thanks to both, @mrjj and @aha_1980 , both your answers did help me to get something going that will bring me further towards my end goal with this project! Your help is appreciated! Thank you!
            a small addition for whoever else will find this thread:
            @aha_1980 's solutions is missing ); at the end, i.e. the complete lambda style solution looks like:

            QObject::connect(checkallPushButton, &QPushButton::clicked, [] {
                QMessageBox msgBox;msgBox.setText("bar!");msgBox.exec();
            });
            
            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @cerr Thanks for the feedback, I fixed it.

            Qt has to stay free or it will die.

            1 Reply Last reply
            2
            • cerrC cerr

              Hi,

              First off: Qt newbie here, excuse the simple question:
              How can I hook trigger a simple function on the click of a button?
              I've added the following to my ui_main.h header file:

              #include <QtWidgets/QMessageBox>
              class Ui_MainWindow
              public:
              ...
              ...
              void setupUi((QMainWindow *MainWindow)
              {
              ...
              ...
              QObject::connect(checkallPushButton, SIGNAL(clicked()), SplintOptionsGroupBox, SLOT(foo1("bar!")));
              }
              ..
              ...
              public slots:
               void foo1(QString msg) {QMessageBox msgBox;msgBox.setText(msg);msgBox.exec();}
              };
              

              and upon click on the checkallPushButton, I would expect a dialog box to pup up with the string bar! but this isn't happening and I'm wondering why...

              Some context: I'm trying to re-use an existing ui (from someone else) but have to first learn how things exactly connect with each other and the above, is what I thought I've read on various pages on the web....
              Thanks for any help/hints to get this working the way I imagine ;)

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @cerr said in Connect a function to a slot (action):

              I've added the following to my ui_main.h header file:

              You should NOT do this as ui_* files are generated and you will loose your changes next time these files are generated again!
              The correct location to do this is in the class to which your form belongs.

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

              1 Reply Last reply
              3
              • cerrC Offline
                cerrC Offline
                cerr
                wrote on last edited by cerr
                #7

                @jsulm said in Connect a function to a slot (action):

                @cerr said in Connect a function to a slot (action):

                I've added the following to my ui_main.h header file:

                You should NOT do this as ui_* files are generated and you will lose your changes next time these files are generated again!
                The correct location to do this is in the class to which your form belongs.

                Right, I've realized this too, the "hard"/stupid way... after losing some of the changes & additions (luckily it wasn't that much) :)

                1 Reply Last reply
                1

                • Login

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