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. Timers cannot be stopped from another thread, but how do i stop/start timer in thread?
QtWS25 Last Chance

Timers cannot be stopped from another thread, but how do i stop/start timer in thread?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 33.4k 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.
  • U Offline
    U Offline
    umen242
    wrote on 26 Jan 2012, 08:41 last edited by
    #1

    I have timer inside thread that invoking function each N seconds .
    now i have in my main window button that supposed stop/start this operation.
    so i just created in my worker thread 2 public function for starting and stooping the timer inside the thread .
    but it don't work , im getting this message :
    @QObject::killTimer: timers cannot be stopped from another thread@
    and when i try to start :
    @QObject::startTimer: timers cannot be started from another thread@
    i want to keep the thread working , and only control on the timer stop/start invocation inside the thread.
    what is the right way to do it ?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      GentooXativa
      wrote on 26 Jan 2012, 08:54 last edited by
      #2

      Use signals to start/stop times, check that "wiki page":http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects

      I hope this help you :)

      Jose Vicente Giner Sanchez - Senior Mobile Developer

      www.gigigo.com

      C/ Dr. Zamenhof 36bis, 1ºA 28027 Madrid
      T: +34 917431436

      1 Reply Last reply
      0
      • U Offline
        U Offline
        umen242
        wrote on 26 Jan 2012, 09:03 last edited by
        #3

        Thanks but i dont see any solution in that page , i do use the timer solution they offer
        but i need to control this timer from outside the thread

        1 Reply Last reply
        0
        • G Offline
          G Offline
          GentooXativa
          wrote on 26 Jan 2012, 09:07 last edited by
          #4

          Add a signal to your thread class, for example
          @
          signal startTimer();
          signal stopTimer();
          @
          And connect it on your main class/thread, emit the signals when you want to start/stop the timer.

          Jose Vicente Giner Sanchez - Senior Mobile Developer

          www.gigigo.com

          C/ Dr. Zamenhof 36bis, 1ºA 28027 Madrid
          T: +34 917431436

          1 Reply Last reply
          0
          • U Offline
            U Offline
            umen242
            wrote on 26 Jan 2012, 09:38 last edited by
            #5

            Thanks , i found the answer simple using the :
            @QMetaObject::invokeMethod(object, "methodName",
            Qt::QueuedConnection,
            Q_ARG(type1, arg1),
            Q_ARG(type2, arg2));
            @

            1 Reply Last reply
            2
            • L Offline
              L Offline
              lgeyer
              wrote on 26 Jan 2012, 09:41 last edited by
              #6

              Be aware that slots are executed in the thread a QObject* has been created or move to, which is usually not the thread that was spawned by subclassing QThread. Thus it is recommended to not subclass QThread but creating a QObject that contains all the functionality and then moving this object to an ordinary QThread.

              @
              class Worker : public QObject
              {
              public:
              Worker(QObject* parent = 0) : QObject(parent), _timer(new QTimer(this))
              {
              ...
              connect(_timer, SIGNAL(timeout()), this, SLOT(work()));
              }

              public slots:
              void startTimer() { _timer->start(); }
              void stopTimer() { _timer->stop(); }

              private slots:
              work()
              {
              ...
              }

              private:
              QTimer* _timer;
              };

              ...

              QThread* thread = new QThread;

              Worker* worker = new Worker;
              worker->moveToThread(thread);
              connect(..., ..., worker, SLOT(startTimer()));
              connect(..., ..., worker, SLOT(stopTimer()));

              thread->start();

              @
              Brain to terminal. Not tested. Exemplary.

              1 Reply Last reply
              0
              • U Offline
                U Offline
                umen242
                wrote on 26 Jan 2012, 11:22 last edited by
                #7

                This is what i did , this is how i set my thread :

                @m_MyThread = new QThread();
                m_MainWorker = new MainWorker();
                connect(m_MyThread,SIGNAL(started()),m_MainWorker,SLOT(doWork()));
                m_MainWorker->moveToThread(m_MyThread);
                m_MyThread->start();@

                1 Reply Last reply
                0
                • J jeremy_k referenced this topic on 12 Jan 2024, 15:36

                1/7

                26 Jan 2012, 08:41

                • Login

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