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. QTimer not triggering slot
QtWS25 Last Chance

QTimer not triggering slot

Scheduled Pinned Locked Moved Solved General and Desktop
qtimerslotssignal & slot
9 Posts 4 Posters 594 Views
  • 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.
  • G Offline
    G Offline
    grinqt
    wrote on 30 Aug 2024, 22:48 last edited by
    #1

    Hi,

    I am able to create a QTimer, start it, see that it is running, but it is not activating my slot. Here is my code:

    #include "desktopserialca.h"
    #include <QDebug>
    #include <QThread>
    
    DesktopSerialCA::DesktopSerialCA(AppDevice appDevice, QObject *parent)
        : QObject{parent}
    {
        QTimer* timer1 = new QTimer(this);
        connect(timer1, &QTimer::timeout, this, &DesktopSerialCA::readSerialData);
        timer1->start(2000);
    
        qDebug() << "Timer id: " << timer1->timerId();
        qDebug() << "Is active: " << timer1->isActive();
        qDebug() << "Remaining time: " << timer1->remainingTime();
    }
    
    void DesktopSerialCA::connectSerialDevice()
    {
    
    }
    
    void DesktopSerialCA::readSerialData() {
        qDebug() << "Slot activated!";
    }
    
    

    .h file:

    #ifndef DESKTOPSERIALCA_H
    #define DESKTOPSERIALCA_H
    
    #include <QObject>
    #include <QSerialPort>
    #include <QSerialPortInfo>
    #include <QTimer>
    #include "appdevice.h"
    #include "testsigslots.h"
    
    class DesktopSerialCA : public QObject
    {
        Q_OBJECT
        QSerialPort serialPort;
        QTimer* timer;
        void connectSerialDevice();
    public:
        explicit DesktopSerialCA(AppDevice appDevice, QObject *parent = nullptr);
    
    signals:
    
    public slots:
        void readSerialData();
    };
    
    #endif // DESKTOPSERIALCA_H
    
    

    main.cpp:

    #include <QApplication>
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QCoreApplication>
    #include <QSerialPort>
    #include <QSerialPortInfo>
    #include <QIODevice>
    
    #include "appdevice.h"
    #include "desktopserialca.h"
    
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        AppDevice appDevice = AppDevice(); // device the app is running on
    
        if (appDevice.isDesktop()) {
            DesktopSerialCA serialCA = DesktopSerialCA(appDevice);
        }
    
        engine.load(QUrl(QStringLiteral("qrc:/Main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
        return app.exec();
    }
    
    

    I'm deploying to Desktop Qt 6.7.0. Here is my application output:

    15:44:04: QML debugging is enabled. Only use this in a safe environment.
    Timer id:  1
    Is active:  true
    Remaining time:  2100
    

    As you can see, the slot is not being triggered. Any help would be appreciated!

    C P 2 Replies Last reply 31 Aug 2024, 00:07
    0
    • G grinqt
      30 Aug 2024, 22:48

      Hi,

      I am able to create a QTimer, start it, see that it is running, but it is not activating my slot. Here is my code:

      #include "desktopserialca.h"
      #include <QDebug>
      #include <QThread>
      
      DesktopSerialCA::DesktopSerialCA(AppDevice appDevice, QObject *parent)
          : QObject{parent}
      {
          QTimer* timer1 = new QTimer(this);
          connect(timer1, &QTimer::timeout, this, &DesktopSerialCA::readSerialData);
          timer1->start(2000);
      
          qDebug() << "Timer id: " << timer1->timerId();
          qDebug() << "Is active: " << timer1->isActive();
          qDebug() << "Remaining time: " << timer1->remainingTime();
      }
      
      void DesktopSerialCA::connectSerialDevice()
      {
      
      }
      
      void DesktopSerialCA::readSerialData() {
          qDebug() << "Slot activated!";
      }
      
      

      .h file:

      #ifndef DESKTOPSERIALCA_H
      #define DESKTOPSERIALCA_H
      
      #include <QObject>
      #include <QSerialPort>
      #include <QSerialPortInfo>
      #include <QTimer>
      #include "appdevice.h"
      #include "testsigslots.h"
      
      class DesktopSerialCA : public QObject
      {
          Q_OBJECT
          QSerialPort serialPort;
          QTimer* timer;
          void connectSerialDevice();
      public:
          explicit DesktopSerialCA(AppDevice appDevice, QObject *parent = nullptr);
      
      signals:
      
      public slots:
          void readSerialData();
      };
      
      #endif // DESKTOPSERIALCA_H
      
      

      main.cpp:

      #include <QApplication>
      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QQmlContext>
      #include <QCoreApplication>
      #include <QSerialPort>
      #include <QSerialPortInfo>
      #include <QIODevice>
      
      #include "appdevice.h"
      #include "desktopserialca.h"
      
      
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
          QQmlApplicationEngine engine;
          AppDevice appDevice = AppDevice(); // device the app is running on
      
          if (appDevice.isDesktop()) {
              DesktopSerialCA serialCA = DesktopSerialCA(appDevice);
          }
      
          engine.load(QUrl(QStringLiteral("qrc:/Main.qml")));
          if (engine.rootObjects().isEmpty())
              return -1;
          return app.exec();
      }
      
      

      I'm deploying to Desktop Qt 6.7.0. Here is my application output:

      15:44:04: QML debugging is enabled. Only use this in a safe environment.
      Timer id:  1
      Is active:  true
      Remaining time:  2100
      

      As you can see, the slot is not being triggered. Any help would be appreciated!

      C Offline
      C Offline
      ChrisW67
      wrote on 31 Aug 2024, 00:07 last edited by
      #2

      In main.cpp:

          if (appDevice.isDesktop()) {
              DesktopSerialCA serialCA = DesktopSerialCA(appDevice);
          }
      

      The lifespan of the DesktopSerialCA object is scoped by the enclosing braces. So, the object gets constructed giving the output you see, and then immediately destroyed.

      1 Reply Last reply
      4
      • G grinqt
        30 Aug 2024, 22:48

        Hi,

        I am able to create a QTimer, start it, see that it is running, but it is not activating my slot. Here is my code:

        #include "desktopserialca.h"
        #include <QDebug>
        #include <QThread>
        
        DesktopSerialCA::DesktopSerialCA(AppDevice appDevice, QObject *parent)
            : QObject{parent}
        {
            QTimer* timer1 = new QTimer(this);
            connect(timer1, &QTimer::timeout, this, &DesktopSerialCA::readSerialData);
            timer1->start(2000);
        
            qDebug() << "Timer id: " << timer1->timerId();
            qDebug() << "Is active: " << timer1->isActive();
            qDebug() << "Remaining time: " << timer1->remainingTime();
        }
        
        void DesktopSerialCA::connectSerialDevice()
        {
        
        }
        
        void DesktopSerialCA::readSerialData() {
            qDebug() << "Slot activated!";
        }
        
        

        .h file:

        #ifndef DESKTOPSERIALCA_H
        #define DESKTOPSERIALCA_H
        
        #include <QObject>
        #include <QSerialPort>
        #include <QSerialPortInfo>
        #include <QTimer>
        #include "appdevice.h"
        #include "testsigslots.h"
        
        class DesktopSerialCA : public QObject
        {
            Q_OBJECT
            QSerialPort serialPort;
            QTimer* timer;
            void connectSerialDevice();
        public:
            explicit DesktopSerialCA(AppDevice appDevice, QObject *parent = nullptr);
        
        signals:
        
        public slots:
            void readSerialData();
        };
        
        #endif // DESKTOPSERIALCA_H
        
        

        main.cpp:

        #include <QApplication>
        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        #include <QQmlContext>
        #include <QCoreApplication>
        #include <QSerialPort>
        #include <QSerialPortInfo>
        #include <QIODevice>
        
        #include "appdevice.h"
        #include "desktopserialca.h"
        
        
        int main(int argc, char *argv[])
        {
            QGuiApplication app(argc, argv);
            QQmlApplicationEngine engine;
            AppDevice appDevice = AppDevice(); // device the app is running on
        
            if (appDevice.isDesktop()) {
                DesktopSerialCA serialCA = DesktopSerialCA(appDevice);
            }
        
            engine.load(QUrl(QStringLiteral("qrc:/Main.qml")));
            if (engine.rootObjects().isEmpty())
                return -1;
            return app.exec();
        }
        
        

        I'm deploying to Desktop Qt 6.7.0. Here is my application output:

        15:44:04: QML debugging is enabled. Only use this in a safe environment.
        Timer id:  1
        Is active:  true
        Remaining time:  2100
        

        As you can see, the slot is not being triggered. Any help would be appreciated!

        P Offline
        P Offline
        Pl45m4
        wrote on 31 Aug 2024, 00:49 last edited by
        #3

        @grinqt

        Fix:

            DesktopSerialCA serialCA;
            if (appDevice.isDesktop()) {
                serialCA = DesktopSerialCA(appDevice);
            }
        

        Also what is AppDevice? Do you really want to pass it by value instead of passing the pointer/a ref?


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        4
        • G Offline
          G Offline
          grinqt
          wrote on 2 Sept 2024, 16:34 last edited by
          #4

          Thank you for the responses, I'm going to read up a bit more on object lifespan to solidify my understanding.

          P 1 Reply Last reply 2 Sept 2024, 16:41
          0
          • G grinqt has marked this topic as solved on 2 Sept 2024, 16:35
          • G grinqt
            2 Sept 2024, 16:34

            Thank you for the responses, I'm going to read up a bit more on object lifespan to solidify my understanding.

            P Offline
            P Offline
            Pl45m4
            wrote on 2 Sept 2024, 16:41 last edited by Pl45m4 9 Feb 2024, 16:43
            #5

            @grinqt

            This is basic and simple C/C++.
            When a scope ends, all locally declared (stack-) variables become invalid.
            (Heap variables as well, but then you leak memory, if you don't clean up properly)


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            G 1 Reply Last reply 2 Sept 2024, 17:22
            1
            • P Pl45m4
              2 Sept 2024, 16:41

              @grinqt

              This is basic and simple C/C++.
              When a scope ends, all locally declared (stack-) variables become invalid.
              (Heap variables as well, but then you leak memory, if you don't clean up properly)

              G Offline
              G Offline
              grinqt
              wrote on 2 Sept 2024, 17:22 last edited by grinqt 9 Feb 2024, 17:25
              #6

              @Pl45m4 I am pretty new to C++. I understand why the code wasn't working in my case, but I need to figure out how to change my code. When I try the code you recommended:

                  DesktopSerialCA serialCA;
                  if (appDevice.isDesktop()) {
                      serialCA = DesktopSerialCA(appDevice);
                  }
              
              

              It does not work since it is trying to call a default constructor here:

              DesktopSerialCA serialCA;
              

              Would you recommend I use a pointer instead? Like so:

                  DesktopSerialCA* serialCA;
                  if (appDevice.isDesktop()) {
                      serialCA = new DesktopSerialCA(appDevice);
                  }
              

              But then where would I delete serialCA? If I do it in main, won't the object be deleted before execution?

              J C 2 Replies Last reply 2 Sept 2024, 18:50
              0
              • G grinqt
                2 Sept 2024, 17:22

                @Pl45m4 I am pretty new to C++. I understand why the code wasn't working in my case, but I need to figure out how to change my code. When I try the code you recommended:

                    DesktopSerialCA serialCA;
                    if (appDevice.isDesktop()) {
                        serialCA = DesktopSerialCA(appDevice);
                    }
                
                

                It does not work since it is trying to call a default constructor here:

                DesktopSerialCA serialCA;
                

                Would you recommend I use a pointer instead? Like so:

                    DesktopSerialCA* serialCA;
                    if (appDevice.isDesktop()) {
                        serialCA = new DesktopSerialCA(appDevice);
                    }
                

                But then where would I delete serialCA? If I do it in main, won't the object be deleted before execution?

                J Offline
                J Offline
                JonB
                wrote on 2 Sept 2024, 18:50 last edited by
                #7

                @grinqt
                Then you should delete it explicitly prior to any return from main(). Or QScopedPointer could do it for you.

                1 Reply Last reply
                2
                • G grinqt
                  2 Sept 2024, 17:22

                  @Pl45m4 I am pretty new to C++. I understand why the code wasn't working in my case, but I need to figure out how to change my code. When I try the code you recommended:

                      DesktopSerialCA serialCA;
                      if (appDevice.isDesktop()) {
                          serialCA = DesktopSerialCA(appDevice);
                      }
                  
                  

                  It does not work since it is trying to call a default constructor here:

                  DesktopSerialCA serialCA;
                  

                  Would you recommend I use a pointer instead? Like so:

                      DesktopSerialCA* serialCA;
                      if (appDevice.isDesktop()) {
                          serialCA = new DesktopSerialCA(appDevice);
                      }
                  

                  But then where would I delete serialCA? If I do it in main, won't the object be deleted before execution?

                  C Offline
                  C Offline
                  ChrisW67
                  wrote on 3 Sept 2024, 04:07 last edited by ChrisW67 9 Mar 2024, 22:21
                  #8

                  @grinqt DesktopSerialCA is a QObject so you could just use the QObject hierarchy to manage its lifetime.

                  DesktopSerialCA* serialCA = nullptr;  // always initialise pointers
                  if (appDevice.isDesktop()) {
                      serialCA = new DesktopSerialCA(appDevice, qApp);
                  }
                  

                  (See qApp if that is unknown to you).

                  If I do it in main, won't the object be deleted before execution?

                  If you explicitly delete after app.exec() returns then you should be good because your GUI is done:

                  ...
                  int returnVal = app.exec();
                  delete serialCA;
                  return returnVal;
                  

                  but @JonB's suggestion of QScopedPointer would be better if you do not use QObject hierarchy.

                  1 Reply Last reply
                  3
                  • G Offline
                    G Offline
                    grinqt
                    wrote on 3 Sept 2024, 20:49 last edited by
                    #9

                    @JonB @ChrisW67 Thanks! I'll use a QScopedPointer!

                    1 Reply Last reply
                    0

                    4/9

                    2 Sept 2024, 16:34

                    • Login

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