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. using QTimer

using QTimer

Scheduled Pinned Locked Moved Solved General and Desktop
qthreadwidget applicatc++timer
3 Posts 2 Posters 553 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.
  • D Offline
    D Offline
    Dean21
    wrote on 9 Jun 2023, 09:23 last edited by
    #1

    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

    D 1 Reply Last reply 9 Jun 2023, 09:43
    0
    • D Dean21
      9 Jun 2023, 09:23

      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

      D Offline
      D Offline
      Dean21
      wrote on 9 Jun 2023, 09:43 last edited by
      #2

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

      L 1 Reply Last reply 26 Jun 2023, 02:41
      0
      • D Dean21 has marked this topic as solved on 9 Jun 2023, 09:43
      • D Dean21
        9 Jun 2023, 09:43

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

        L Offline
        L Offline
        luebleton
        wrote on 26 Jun 2023, 02:41 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0

        1/3

        9 Jun 2023, 09:23

        • Login

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