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. Using QTimer with signal
Forum Updated to NodeBB v4.3 + New Features

Using QTimer with signal

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 1.4k 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.
  • A Offline
    A Offline
    Alpkan
    wrote on 18 Mar 2021, 08:44 last edited by
    #1

    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

    J 1 Reply Last reply 18 Mar 2021, 08:46
    0
    • A Alpkan
      18 Mar 2021, 08:44

      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

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 18 Mar 2021, 08:46 last edited by
      #2

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

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

      A 1 Reply Last reply 18 Mar 2021, 09:11
      7
      • A Offline
        A Offline
        Alpkan
        wrote on 18 Mar 2021, 09:00 last edited by
        #3

        thanks for your answer, it's solved the problem

        1 Reply Last reply
        0
        • J jsulm
          18 Mar 2021, 08:46

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

          A Offline
          A Offline
          Alpkan
          wrote on 18 Mar 2021, 09:11 last edited by
          #4

          @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

          J 1 Reply Last reply 18 Mar 2021, 09:14
          0
          • A Alpkan
            18 Mar 2021, 09:11

            @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

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 18 Mar 2021, 09:14 last edited by
            #5

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

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

            1 Reply Last reply
            2
            • A Offline
              A Offline
              Alpkan
              wrote on 18 Mar 2021, 09:24 last edited by
              #6

              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.

              J 1 Reply Last reply 18 Mar 2021, 09:27
              0
              • A Alpkan
                18 Mar 2021, 09:24

                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.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 18 Mar 2021, 09:27 last edited by
                #7

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

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

                A 1 Reply Last reply 18 Mar 2021, 10:48
                2
                • J jsulm
                  18 Mar 2021, 09:27

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

                  A Offline
                  A Offline
                  Alpkan
                  wrote on 18 Mar 2021, 10:48 last edited by
                  #8

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

                  J.HilkJ 1 Reply Last reply 18 Mar 2021, 13:18
                  0
                  • A Alpkan
                    18 Mar 2021, 10:48

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

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on 18 Mar 2021, 13:18 last edited by
                    #9

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

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

                    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
                    • A Offline
                      A Offline
                      Alpkan
                      wrote on 18 Mar 2021, 13:35 last edited by
                      #10

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

                      J.HilkJ 1 Reply Last reply 18 Mar 2021, 13:39
                      0
                      • A Alpkan
                        18 Mar 2021, 13:35

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

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on 18 Mar 2021, 13:39 last edited by J.Hilk
                        #11

                        @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


                        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
                        2

                        1/11

                        18 Mar 2021, 08:44

                        • Login

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