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. Issues adding QTimer to a custom class
Forum Update on Tuesday, May 27th 2025

Issues adding QTimer to a custom class

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 633 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.
  • KenAppleby 0K KenAppleby 0

    @lukutis222 You're using command_timeout() as a slot but declaring it as a signal.

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

    @KenAppleby-0
    No, I thought that initially. But Qt allows signal connections to "chain" to other signals, so I think command_timeout() as a signal is fine.

    KenAppleby 0K 1 Reply Last reply
    1
    • L lukutis222

      I have created custom Serial class where I created functions for handling serial communication. I would also like to create QTimer there but I got some strange issues:

      C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: debug/main.o:main.cpp:(.rdata$.refptr._ZTV6Serial[.refptr._ZTV6Serial]+0x0): undefined reference to `vtable for Serial'
      C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: debug/serial.o:serial.cpp:(.rdata$.refptr._ZN6Serial16staticMetaObjectE[.refptr._ZN6Serial16staticMetaObjectE]+0x0): undefined reference to `Serial::staticMetaObject'
      collect2.exe: error: ld returned 1 exit status
      mingw32-make[1]: *** [Makefile.Debug:82: debug/uCurrent.exe] Error 1
      mingw32-make[1]: Leaving directory 'C:/Users/petrikas.lu/Desktop/WORK/QT/uCurrent/build-uCurrent-Desktop_Qt_6_4_0_MinGW_64_bit-Debug'
      mingw32-make: *** [Makefile:45: debug] Error 2
      12:28:28: The process "C:\Qt\Tools\mingw1120_64\bin\mingw32-make.exe" exited with code 2.
      Error while building/deploying project uCurrent (kit: Desktop Qt 6.4.0 MinGW 64-bit)
      When executing step "Make"
      
      ![f32144d9-793e-4b2d-82bb-2d3e5ca09ce3-image.png](https://ddgobkiprc33d.cloudfront.net/7ec7ef65-43fd-4743-9856-c8f59d2a44cd.png) 
      
      

      My class constructor in serial.cpp file

      Serial::Serial(QObject *parent)
      : QObject(parent)
      {
      
          test_timer = new QTimer(0);
          QObject::connect(test_timer, &QTimer::timeout, this, &Serial::command_timeout);
      
      }
      

      And my serial.h is as following:

      #ifndef SERIAL_H
      #define SERIAL_H
      
      
      #include "QSerialPort"
      #include "QSerialPortInfo"
      #include <qDebug>
      #include <QTimer>
      
      
      
      
      class Serial : public QObject
      {
          Q_OBJECT
          
      public:
          Serial(QObject *parent = 0);
          QTimer *test_timer; // timeout that is responsible for waiting for any serial response.
          QSerialPort serial_connection; // this keeps all information regarding the serial connection
      
      
          void Scan_serial_devices();
          bool Serial_connect(QSerialPort* port);
          bool Serial_disconnect(QSerialPort* port);
      
          void write_data(QSerialPort* port,QByteArray data);
          bool is_data_available(QSerialPort* port);
          QByteArray read_data(QSerialPort* port);
          bool Match_serial_string(QString data_received,QString string_to_match);
      
      
      
      
      
          void Set_Baudrate(QSerialPort* port,int32_t baudrate);
          void Set_Portname(QSerialPort* port,QString name);
          void Set_Databits(QSerialPort* port,QSerialPort::DataBits dataBits);
          void Set_Stopbits(QSerialPort* port,QSerialPort::StopBits stopBits);
          void Set_Flowcontrol(QSerialPort* port,QSerialPort::FlowControl flowControl);
          void Set_Paritybits(QSerialPort* port,QSerialPort::Parity parity);
      
      signals:
          void command_timeout();
      
      
      private:
      
      
      protected:
      
      
      };
      
      #endif // SERIAL_H
      

      I would appreciate if someone could help me detect mistake in my code.

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

      @lukutis222
      Did you perchance only just add the Q_OBJECT macro into class Serial? I would start by deleting all files in the build output directory and recompile from scratch, does that still produce the same linker error?

      If not can we see your Serial code in main.cpp?

      Christian EhrlicherC L 2 Replies Last reply
      0
      • JonBJ JonB

        @lukutis222
        Did you perchance only just add the Q_OBJECT macro into class Serial? I would start by deleting all files in the build output directory and recompile from scratch, does that still produce the same linker error?

        If not can we see your Serial code in main.cpp?

        Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #5

        vtable -> moc does not generate the moc_foo.cpp. Since you did not tell us your buildsystem you have to fiddle out by yourself how you can run moc on your headers.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • JonBJ JonB

          @lukutis222
          Did you perchance only just add the Q_OBJECT macro into class Serial? I would start by deleting all files in the build output directory and recompile from scratch, does that still produce the same linker error?

          If not can we see your Serial code in main.cpp?

          L Offline
          L Offline
          lukutis222
          wrote on last edited by
          #6

          @JonB Yes I manually added Q_OBJECT macro into class Serial.

          Deleting whole build folder and building the project again solved the issue. Thank you very much

          JonBJ 1 Reply Last reply
          0
          • L lukutis222

            @JonB Yes I manually added Q_OBJECT macro into class Serial.

            Deleting whole build folder and building the project again solved the issue. Thank you very much

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

            @lukutis222 said in Issues adding QTimer to a custom class:

            @JonB Yes I manually added Q_OBJECT macro into class Serial.

            The rule is any time you add Q_OBJECT macro you must force a complete rebuild. Else you will get "strange" linker errors. That's just how it is. At least for qmake, I don't know about the cmake situation.

            L Christian EhrlicherC 2 Replies Last reply
            1
            • JonBJ JonB

              @lukutis222 said in Issues adding QTimer to a custom class:

              @JonB Yes I manually added Q_OBJECT macro into class Serial.

              The rule is any time you add Q_OBJECT macro you must force a complete rebuild. Else you will get "strange" linker errors. That's just how it is. At least for qmake, I don't know about the cmake situation.

              L Offline
              L Offline
              lukutis222
              wrote on last edited by
              #8

              @JonB Thanks for confirming this! :)

              1 Reply Last reply
              1
              • JonBJ JonB

                @KenAppleby-0
                No, I thought that initially. But Qt allows signal connections to "chain" to other signals, so I think command_timeout() as a signal is fine.

                KenAppleby 0K Offline
                KenAppleby 0K Offline
                KenAppleby 0
                wrote on last edited by
                #9

                @JonB Thanks, I didn't know that. It's useful.

                JonBJ 1 Reply Last reply
                1
                • JonBJ JonB

                  @lukutis222 said in Issues adding QTimer to a custom class:

                  @JonB Yes I manually added Q_OBJECT macro into class Serial.

                  The rule is any time you add Q_OBJECT macro you must force a complete rebuild. Else you will get "strange" linker errors. That's just how it is. At least for qmake, I don't know about the cmake situation.

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @JonB said in Issues adding QTimer to a custom class:

                  I don't know about the cmake situation.

                  CMake when using automoc does not need anything special, a simple compile will correctly create the needed moc calls.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  2
                  • KenAppleby 0K KenAppleby 0

                    @JonB Thanks, I didn't know that. It's useful.

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

                    @KenAppleby-0
                    Yes, of course you could always raise a subsequent signal by connecting the first signal to a slot method or lambda which emits the second signal but Qt allows to connect directly to another signal for "chaining". I don't know whether this is documented,

                    jeremy_kJ 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @KenAppleby-0
                      Yes, of course you could always raise a subsequent signal by connecting the first signal to a slot method or lambda which emits the second signal but Qt allows to connect directly to another signal for "chaining". I don't know whether this is documented,

                      jeremy_kJ Offline
                      jeremy_kJ Offline
                      jeremy_k
                      wrote on last edited by
                      #12

                      @JonB said in Issues adding QTimer to a custom class:

                      @KenAppleby-0
                      Yes, of course you could always raise a subsequent signal by connecting the first signal to a slot method or lambda which emits the second signal but Qt allows to connect directly to another signal for "chaining". I don't know whether this is documented,

                      It is documented, in the second to last paragraph of the Signals and Slots section.

                      You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)

                      Asking a question about code? http://eel.is/iso-c++/testcase/

                      1 Reply Last reply
                      3

                      • Login

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