Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Static slot function

    General and Desktop
    5
    8
    708
    Loading More Posts
    • 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.
    • E
      Everdream last edited by Everdream

      Hello,

      I'm trying to connect a QButtonGroup signal with a static slot function like this:

      QObject::connect(ui->ModeGroup, SIGNAL(buttonClicked(int)),SLOT(module::toggle_mode(int)));
      

      But the slot does not answer to the signal.

      I tried that too:

      QObject::connect(ui->ModeGroup, &QButtonGroup::buttonClicked,&module::toggle_mode);
      

      This version gave me an error when compiling. I might think that is looking for a slot function with a QPushButton* as input. But i'd like to use the overload buttonClicked(int) signal.

      The error I got from the last method is:

      mainwindow.cpp:65:18: error: no matching member function for call to 'connect'
      qobject.h:463:41: note: candidate function not viable: no overload of 'buttonClicked' matching 'const char *' for 2nd argument
      qobject.h:260:13: note: candidate template ignored: couldn't infer template argument 'Func1'
      qobject.h:300:13: note: candidate template ignored: couldn't infer template argument 'Func1'
      qobject.h:228:43: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
      qobject.h:269:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
      qobject.h:308:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided
      qobject.h:208:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided
      qobject.h:211:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided
      

      Is my writting wrong?

      By the way, I'm using Qt 5.12.8.

      Thanks,

      Eve

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @Everdream last edited by

        @Everdream said in Static slot function:

        buttonClicked

        Proper signature of the signal is

        void QButtonGroup::buttonClicked(QAbstractButton*)
        

        not

        void QButtonGroup::buttonClicked(int)
        

        I don't think you can use a static method as slot. Also, why do you want to do so?

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

        1 Reply Last reply Reply Quote 0
        • E
          Everdream last edited by Everdream

          Well, when I do that:
          QObject::connect(ui->ModeGroup, SIGNAL(buttonClicked(int)),scvG,SLOT(toggle_mode(int)));

          Using an instance pointer of the **module ** class, it's working. There is an overload method of buttonClicked with the int ID of the button clicked.

          I put it static since the attribut modified by the slot is also static.

          I think, it's kinda weird to mention a pointer to an instance to modify a static variable.

          Well, I think, I'll go for that. At least it's working.

          JonB J.Hilk 2 Replies Last reply Reply Quote 0
          • JonB
            JonB @Everdream last edited by JonB

            @Everdream
            If you are having problems with a static function as a slot, connect it via a lambda. Though e.g. https://stackoverflow.com/questions/9428038/is-it-possible-to-connect-a-signal-to-a-static-slot-without-a-receiver-instance shows you that you can anyway.

            Also, why are you now going back to old-style signal/slot connections when you started out using the much better new-style? You will be better served if you always stick to new-style.

            1 Reply Last reply Reply Quote 1
            • J.Hilk
              J.Hilk Moderators @Everdream last edited by

              @Everdream said in Static slot function:

              I think, it's kinda weird to mention a pointer to an instance to modify a static variable.

              you shouldn't,

              this for example works fine

              int main(int argc, char *argv[])
              {
                  QApplication app(argc, argv);
              
                  QTimer *t(new QTimer(Q_NULLPTR));
                  QObject::connect(t, &QTimer::timeout, &myWidget::testFunction);
                  t->start(200);
              
              
                  return app.exec();
              }
              
              class myWidget : public QWidget
              {
                  Q_OBJECT
              public:
                  myWidget(QWidget * parent = nullptr) : QWidget(parent){
                  }
              
              
                  static void testFunction() {qDebug() << "TestPrint "<< QTime::currentTime();}
              
              };
              

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

              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

              1 Reply Last reply Reply Quote 1
              • E
                Everdream last edited by

                Ok I found a solution to get it working:

                QObject::connect(ui->ModeGroup, qOverload<int>(&QButtonGroup::buttonClicked),&module::toggle_mode);
                

                Since the buttonclicked is overloaded signal. It was always trying to connect with the other buttonClicked(QAbstractButton*), qOverload here forced the use of the right signal.

                @JonB I tried your other solution, with the lambda function, I didn't succeed to make it work.

                Thank you everyone.

                You can make the post as solved.

                JonB Pablo J. Rogina 2 Replies Last reply Reply Quote 0
                • JonB
                  JonB @Everdream last edited by

                  @Everdream said in Static slot function:

                  @JonB I tried your other solution, with the lambda function, I didn't succeed to make it work.

                  Up to you, lambdas do work. But if you have a solution without you don't need them, just bear in mind for next time.

                  1 Reply Last reply Reply Quote 0
                  • Pablo J. Rogina
                    Pablo J. Rogina @Everdream last edited by

                    @Everdream said in Static slot function:

                    Ok I found a solution to get it working:

                    great, and thank you for sharing it. Please don't forget to mark your post as solved then.

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post