Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Static slot function



  • 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


  • Qt Champions 2019

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



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



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


  • Moderators

    @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();}
    
    };
    


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



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



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


Log in to reply