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

Using QTimer with signal



  • resim_2021-03-18_114255.png

    as you can on the picture when i am trying use qtimer with my signal it's not letting me to do

    is there any solution for it


  • Lifetime Qt Champion

    @Alpkan add this to []
    Reason: the signal belongs to the instance of the class, so you have to capture this in your lambda.



  • thanks for your answer, it's solved the problem



  • @jsulm said in Using QTimer with signal:

    @Alpkan add this to []
    Reason: the signal belongs to the instance of the class, so you have to capture this in your lambda.

    @jsulm that is solved the warnings but now when i run the code, there no message on the screen


  • Lifetime Qt Champion

    @Alpkan Is a slot connected to the mySignal signal?
    And there is another issue in your code: timer is a LOCAL variable which is destroyed when send() terminates.



  • resim_2021-03-18_121318.png

    "signaltaker.h"

    #ifndef SIGNALTAKER_H
    #define SIGNALTAKER_H

    #include <QObject>
    #include <QWidget>
    #include <QDebug>

    class signaltaker:public QObject
    {
    Q_OBJECT
    public:
    explicit signaltaker(QObject *parent = nullptr);

    signals:

    public slots:
    void onMessage(QString message);
    };

    #endif // SIGNALTAKER_H

    "signaltaker.cpp"
    #include "signaltaker.h"

    signaltaker::signaltaker(QObject *parent) : QObject(parent)
    {

    }

    void signaltaker::onMessage(QString message)
    {
    qInfo() << message;

    }

    "widget1.h"
    #ifndef WIDGET1_H
    #define WIDGET1_H

    #include <QObject>
    #include <QWidget>
    #include <QDebug>

    class widget1 : public QObject
    {
    Q_OBJECT
    //here - private
    public:

    //public
    explicit widget1(QObject *parent = nullptr);
    
    void send();
    

    signals:
    void mySignal(QString message);

    public slots:

    };

    #endif // WIDGET1_H

    "widget1.cpp"
    #include "widget1.h"
    #include <QDebug>
    #include <QtCore>
    #include <QTimer>

    widget1::widget1(QObject *parent) : QObject(parent)
    {

    }

    void widget1::send()
    {
    QTimer timer;
    QObject::connect(&timer, &QTimer::timeout, this { emit mySignal("bbbbbbb"); });
    timer.start(2000);

    }

    "widget2.h"
    #ifndef WIDGET2_H
    #define WIDGET2_H

    #include <QObject>
    #include <QWidget>

    class widget2:public QObject
    {
    Q_OBJECT
    public:

    explicit widget2(QObject *parent = nullptr);
    
    void send();
    

    signals:
    void mySignal(QString message);

    public slots:
    };

    #endif // WIDGET2_H

    "widget2.cpp"
    #include "widget2.h"
    #include <QDebug>
    #include <QTimer>
    #include <QtCore>

    widget2::widget2(QObject *parent) : QObject(parent)
    {

    }

    void widget2::send()
    {
    emit mySignal("aaaaaaa");

    }

    "main.cpp"
    #include <QCoreApplication>
    #include <QtCore>
    #include <QTimer>
    #include "widget1.h"
    #include "widget2.h"
    #include "signaltaker.h"

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    widget1 obj1widget1;
    widget2 obj2widget2;
    signaltaker objsignaltaker;
    
    
    QObject::connect(&obj1widget1, &widget1::mySignal, &objsignaltaker, &signaltaker::onMessage);
    QObject::connect(&obj2widget2, &widget2::mySignal, &objsignaltaker, &signaltaker::onMessage);
    
    obj1widget1.send();
    obj2widget2.send();
    
    
    
    
    
    return a.exec();
    

    }

    there is no another issue when i build, it seems every thing is right.


  • Lifetime Qt Champion

    @Alpkan said in Using QTimer with signal:

    there is no another issue when i build, it seems every thing is right.

    Not everything. As I said: timer is a LOCAL variable.

    void widget1::send()
    {
        QTimer timer; // timer is a local variable inside send()
        QObject::connect(&timer, &QTimer::timeout, this { emit mySignal("bbbbbbb"); });
        timer.start(2000); // It gets destroyed just after this line!
    }
    

    Please read about scope in C++ to avoid such mistakes.
    To fix the issue make timer a class variable insider widget1, or allocate it on the heap using "new".



  • @jsulm i understand, thanks for help. I will do it.


  • Moderators

    @Alpkan I think you may want this as a single shot timer, if thats the case:

    QTimer::singleShot(2000, this , [=]()->void{ emit mySignal("bbbbbbb"); });
    


  • I now singleshot is working but i want to send message every 10 second with qtimer :)


  • Moderators

    @Alpkan said in Using QTimer with signal:

    I now singleshot is working but i want to send message every 10 second with qtimer :)

    then you'll need a QTimer as a class member and/or one created on the heap
    and you need to change your timeout from 2 seconds to 10, of course :D


Log in to reply