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. Static slot function
Forum Updated to NodeBB v4.3 + New Features

Static slot function

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 1.6k Views 1 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.
  • E Offline
    E Offline
    Everdream
    wrote on last edited by Everdream
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • E 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

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

      @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
      0
      • E Offline
        E Offline
        Everdream
        wrote on last edited by Everdream
        #3

        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.

        JonBJ J.HilkJ 2 Replies Last reply
        0
        • E 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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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
          1
          • E 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.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @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


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

            1 Reply Last reply
            1
            • E Offline
              E Offline
              Everdream
              wrote on last edited by
              #6

              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.

              JonBJ Pablo J. RoginaP 2 Replies Last reply
              0
              • E Everdream

                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.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @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
                0
                • E Everdream

                  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.

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by
                  #8

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

                  • Login

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