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. QTimer - Problem when passing with passing the values

QTimer - Problem when passing with passing the values

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 1.6k 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
    DdsS
    wrote on last edited by
    #1

    Hi,
    I am a new at Qt. I am facing the problem with QTimer and after hours of studying Qdocumentation and days of trying to figure out I am conufsed. I've search all the topics of QTimer on this forum, but I did not find the answer. So here it is:
    I am currently using a Qt 5.12.0 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit
    my OS is Linux mint. I have a problem when I am passing a variable provided in Gui (qml) to Qtimer in C++. When i provide let say 5s to timer's interval it works, then I am passing 1s and it also works good, but when I provide 5s the timer still works like the 1s was provided. When I use set singleshot(true) the problem does not occur. Here is the code :

    c++:
    void DTimer::timeIn(QString Time)
    {
    qDebug()<<"Timer's time :"<<Time;
    iTime=Time.toInt()*1000;
    timer=new QTimer(this);
    if(iTime>0){
    timer->start(iTime);

    connect(timer, SIGNAL(timeout()),
            this, SLOT(timerOut()));
    }
    

    }

    qml

    TextField{id:timeSet
    anchors.centerIn: zadanyCzas
    width: 40
    height: 20

                                             onTextChanged: obtainTime.getData(timeSet.text);
                                            }
    
    aha_1980A 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You are recreating a new timer each time you call timeIn which is likely not what you want. Unless it supposed to be a single shot timer ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • D Offline
        D Offline
        DdsS
        wrote on last edited by
        #3

        I've created a class and this timeIn is a method. Do you know what should I do to make it works wright. You're right I don't want it to be a single shot timer. I'd like it to be something like in plc timer.

        1 Reply Last reply
        0
        • D DdsS

          Hi,
          I am a new at Qt. I am facing the problem with QTimer and after hours of studying Qdocumentation and days of trying to figure out I am conufsed. I've search all the topics of QTimer on this forum, but I did not find the answer. So here it is:
          I am currently using a Qt 5.12.0 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit
          my OS is Linux mint. I have a problem when I am passing a variable provided in Gui (qml) to Qtimer in C++. When i provide let say 5s to timer's interval it works, then I am passing 1s and it also works good, but when I provide 5s the timer still works like the 1s was provided. When I use set singleshot(true) the problem does not occur. Here is the code :

          c++:
          void DTimer::timeIn(QString Time)
          {
          qDebug()<<"Timer's time :"<<Time;
          iTime=Time.toInt()*1000;
          timer=new QTimer(this);
          if(iTime>0){
          timer->start(iTime);

          connect(timer, SIGNAL(timeout()),
                  this, SLOT(timerOut()));
          }
          

          }

          qml

          TextField{id:timeSet
          anchors.centerIn: zadanyCzas
          width: 40
          height: 20

                                                   onTextChanged: obtainTime.getData(timeSet.text);
                                                  }
          
          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @DdsS please show your whole code. the problem is likely outside these fragments.

          Qt has to stay free or it will die.

          1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Make your QTimer object a member of your class and only call start with the new value.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • D Offline
              D Offline
              DdsS
              wrote on last edited by
              #6

              header:
              #ifndef DTIMER_H
              #define DTIMER_H
              #include <QObject>
              #include <QString>
              #include <QDebug>
              #include <QTimer>
              class DTimer : public QObject
              {
              Q_OBJECT

              public:
              Q_PROPERTY(QString sTime MEMBER sTime NOTIFY timeChanged)
              explicit DTimer(QObject *parent = nullptr);

              QTimer *timer;
              QString sTime;
              

              signals:
              void timeChanged();

              public slots:
              void timerOut();
              void timeIn(QString Time);

              private:

                  int iTime;
              

              };

              #endif // DTIMER_H

              source:
              #include "DTimer.h"

              DTimer::DTimer(QObject *parent) : QObject(parent)
              {
              }

              void DTimer::timeIn(QString Time)
              {
              qDebug()<<"Pobrano czas timera:"<<Time;
              iTime=Time.toInt()*1000;
              timer=new QTimer(this);
              if(iTime>0){
              timer->start(iTime);

              connect(timer, SIGNAL(timeout()),
                      this, SLOT(timerOut()));
              }
              

              }

              void DTimer::timerOut()
              {
              qDebug()<<"Timer shoots";
              }

              Main cpp:

              ObtainmentData ObtainTime;
              DTimer dTimer;

                     engine.rootContext()->setContextProperty("obtainTime", &ObtainTime);
              
                     QObject::connect(&ObtainTime, SIGNAL(sendData(QString)),
                                      &dTimer, SLOT(timeIn(QString)));
              

              qml

              TextField{id:timeSet
              anchors.centerIn: zadanyCzas
              width: 40
              height: 20

                            onTextChanged: obtainTime.getData(timeSet.text);
                           }
              

              This is the whole code for the QTimer

              aha_1980A 1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                Hi
                I would move
                timer=new QTimer(this);
                connect(timer, SIGNAL(timeout()), this, SLOT(timerOut()));
                to constructor of DTimer

                1 Reply Last reply
                2
                • D DdsS

                  header:
                  #ifndef DTIMER_H
                  #define DTIMER_H
                  #include <QObject>
                  #include <QString>
                  #include <QDebug>
                  #include <QTimer>
                  class DTimer : public QObject
                  {
                  Q_OBJECT

                  public:
                  Q_PROPERTY(QString sTime MEMBER sTime NOTIFY timeChanged)
                  explicit DTimer(QObject *parent = nullptr);

                  QTimer *timer;
                  QString sTime;
                  

                  signals:
                  void timeChanged();

                  public slots:
                  void timerOut();
                  void timeIn(QString Time);

                  private:

                      int iTime;
                  

                  };

                  #endif // DTIMER_H

                  source:
                  #include "DTimer.h"

                  DTimer::DTimer(QObject *parent) : QObject(parent)
                  {
                  }

                  void DTimer::timeIn(QString Time)
                  {
                  qDebug()<<"Pobrano czas timera:"<<Time;
                  iTime=Time.toInt()*1000;
                  timer=new QTimer(this);
                  if(iTime>0){
                  timer->start(iTime);

                  connect(timer, SIGNAL(timeout()),
                          this, SLOT(timerOut()));
                  }
                  

                  }

                  void DTimer::timerOut()
                  {
                  qDebug()<<"Timer shoots";
                  }

                  Main cpp:

                  ObtainmentData ObtainTime;
                  DTimer dTimer;

                         engine.rootContext()->setContextProperty("obtainTime", &ObtainTime);
                  
                         QObject::connect(&ObtainTime, SIGNAL(sendData(QString)),
                                          &dTimer, SLOT(timeIn(QString)));
                  

                  qml

                  TextField{id:timeSet
                  anchors.centerIn: zadanyCzas
                  width: 40
                  height: 20

                                onTextChanged: obtainTime.getData(timeSet.text);
                               }
                  

                  This is the whole code for the QTimer

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by aha_1980
                  #8

                  @DdsS you already have a member *timer, but don't use it. Create this timer in the constructor as @SGaist said. remove the new QTimer line from the slot timeIn. It should work.

                  Edit: @mrjj is right. Multiple connects will lead to such phenomena. The slot is called from multiple timers!

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  2
                  • D Offline
                    D Offline
                    DdsS
                    wrote on last edited by
                    #9

                    Thank you Guys for your reply. It works. I was struggle with it for a couple days your answers are very valuable . How ever I Did not find anything about moving it to constructor in documentation (maybe it was my lack of knowledge).

                    mrjjM 1 Reply Last reply
                    0
                    • D DdsS

                      Thank you Guys for your reply. It works. I was struggle with it for a couple days your answers are very valuable . How ever I Did not find anything about moving it to constructor in documentation (maybe it was my lack of knowledge).

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @DdsS
                      Hi
                      Well, the doc wont mention it as it applies to all dynamic object creation. ( not just a timer)
                      If you want to only create one instance (of any class) , you should only new it once and hence the constructor
                      is good place.
                      The reason to move the connect is Qt support connecting multiple times, even to same objects so
                      often you only run the connect statement once as not to get more than one connection.

                      So often you want to keep any connect statement near where you create the object.

                      1 Reply Last reply
                      1
                      • D Offline
                        D Offline
                        DdsS
                        wrote on last edited by
                        #11

                        Thank You very much for Your advise. I'll remeber it. This is very helpful.

                        1 Reply Last reply
                        0

                        • Login

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