using QTimer
-
So I am using MQTT and want to use a timer to wait a set amount of time before doing something, I have used this method in different functions in my code that come after the widget destructor. But when I use this method as shown in the code below I get a debug message saying
"Timers can only be used with threads started with QThread"Ihave tried a couple other methods but couldnt get it to work, and I need it to be a timer not a delay as I want the rest of the program to function while waiting.
#include "widget.h" #include "./ui_widget.h" #include <QPalette> #include <QDebug> #include <QTimer> #include <QString> #include <string> #include <cstring> #include <iostream> #include <time.h> #include </home/dave/mosquitto/include/mosquitto.h> //needed for mosquitto MQTT #include <QApplication> //-----for qt qrapper for mosquitto----- #include <QCoreApplication> #include <QTime> #include "QMMqttClient.h" //-------------------------------------- #include <QThread> //------------IP Adresses------------ char RPi_IP[] = "xxxxxxx"; char Linux_PC_IP[] = "xxxxxxxx"; //----------------------------------- Widget::Widget(QWidget *parent): QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); /* An example of unsecure connection */ client.initialize("12", RPi_IP, 1883); //initialise connection with ID 12, R_Pi IP, and port 1883 client.connect(); // connect with above parameters client.publishMesage("Closed", "0"); //----------------------function to connect to the raspberry pi---------------------- QObject::connect(&client, &QMMqttClient::onConnected, this ,[this](){ qDebug() << " QMMqttClient::onConnected handler, subscribe to topic..."; client.subscribeTopic("Temp Alarm"); }); //----------------------------------------------------------------------------------- QObject::connect(&client, &QMMqttClient::onMessageReceived, [this](const QString &topic, const QByteArray &msg) { ui->Alarm_Triggered_Label->setText("Temperature alarm of PSU triggered, HV output disabled for 5 minutes"); ui->HV_ON_OFF_Button->setEnabled(false); QTimer::singleShot(3000, this, [this](){//publish after 3 seconds client.publishMesage("Alarm_5_min_cooldown_complete", "1"); }); }); } Widget::~Widget() { delete ui; }
Any help would be great,
Thanks,
Dean -
@Dean21 I just figured this out myself and am just posting in case someone else has the same issue.
My function where I want the timer now looks like shown below:
QObject::connect(&client, &QMMqttClient::onMessageReceived, [this](const QString &topic, const QByteArray &msg) { ui->Alarm_Triggered_Label->setText("Temperature alarm of PSU triggered, HV output disabled for 5 minutes"); ui->HV_ON_OFF_Button->setEnabled(false); // Use QMetaObject::invokeMethod to execute the code in the main GUI thread QMetaObject::invokeMethod(this, "publishAfterDelay", Qt::QueuedConnection); });
And the call to function publishAfterDelay() looks like shown below:
void Widget::publishAfterDelay() { QTimer::singleShot(3000, this, [this]() { client.publishMesage("Alarm_5_min_cooldown_complete", "1"); qDebug() << "timer pinged"; }); }
Hope this helps anyone else.
-