QTimer not triggering slot
-
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!
-
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.
-
-
@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?
-
@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.