Issues adding QTimer to a custom class
-
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"

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

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.
@lukutis222 You're using command_timeout() as a slot but declaring it as a signal.
-
@lukutis222 You're using command_timeout() as a slot but declaring it as a signal.
@KenAppleby-0
No, I thought that initially. But Qt allows signal connections to "chain" to other signals, so I thinkcommand_timeout()
as asignal
is fine. -
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"

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.
@lukutis222
Did you perchance only just add theQ_OBJECT
macro intoclass 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 inmain.cpp
? -
@lukutis222
Did you perchance only just add theQ_OBJECT
macro intoclass 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 inmain.cpp
?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.
-
@lukutis222
Did you perchance only just add theQ_OBJECT
macro intoclass 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 inmain.cpp
?@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
-
@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
@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. -
@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.@JonB Thanks for confirming this! :)
-
@KenAppleby-0
No, I thought that initially. But Qt allows signal connections to "chain" to other signals, so I thinkcommand_timeout()
as asignal
is fine.@JonB Thanks, I didn't know that. It's useful.
-
@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.@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.
-
@JonB Thanks, I didn't know that. It's useful.
@KenAppleby-0
Yes, of course you could always raise a subsequent signal by connecting the first signal to a slot method or lambda whichemit
s the second signal but Qt allows to connect directly to another signal for "chaining". I don't know whether this is documented, -
@KenAppleby-0
Yes, of course you could always raise a subsequent signal by connecting the first signal to a slot method or lambda whichemit
s the second signal but Qt allows to connect directly to another signal for "chaining". I don't know whether this is documented,@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 whichemit
s 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.)