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 & Thread, adding it to running thread...
QtWS25 Last Chance

QTimer & Thread, adding it to running thread...

Scheduled Pinned Locked Moved Solved General and Desktop
qthreadqtimer
5 Posts 2 Posters 851 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
    Dariusz
    wrote on 6 May 2021, 19:03 last edited by
    #1

    Hey

    I'm trying to add timers to a running thread so that I can have more options when I play with my worker... here is my broken idea >

                auto item = new QTimer();
                item->setInterval(interval);
                //QMetaObject::invokeMethod(item, [this, i = item]() {
                //item->setParent(nullptr);
                item->moveToThread(this);
                //}, Qt::QueuedConnection);
                QMetaObject::invokeMethod(item->thread(), [this, i = item]() {
                    qDebug() << "Gonna run timer!";
                    i->start();
                    qDebug() << "Gonna run timer!";
                    });
                mExtraTimers.append(item);
                int id = mExtraTimers.size();
                qDebug() << item<<item->thread() << (QObject*)this;
                connect(this, &QThread::finished, item, &QObject::deleteLater, Qt::DirectConnection);
                connect(item, &QTimer::timeout, this, [this, i = id]() { Q_EMIT extraTriggerAction(i); }, Qt::DirectConnection);
                return id;
    

    The idea is that I can add extra timers with different intervals to call a signal. Then when using direcConnection I can execute code depending on id of timer and do some processing...
    But whatever I try I can't start the timer... I keep getting QObject::startTimer: Timers can only be used with threads started with QThread
    How to bite it ?

    TIA

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dariusz
      wrote on 6 May 2021, 19:58 last edited by
      #5

      @SGaist said in QTimer & Thread, adding it to running thread...:

      With only bits of your code it's not possible to understand your implementation and the possible issues you might be facing.

      Today is special day for my brain and I literally fail at basics... I forgot to start the worker... thus its event loop never run... thus it never started the damn timmer... ehhh!!!!

      Everything works fine once I started the worker... yayyyy I learned something new! :- )

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 6 May 2021, 19:11 last edited by
        #2

        Hi,

        When are calling this ?
        Do you have the main event loop running ?

        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
        0
        • D Offline
          D Offline
          Dariusz
          wrote on 6 May 2021, 19:31 last edited by
          #3

          @SGaist said in QTimer & Thread, adding it to running thread...:

          Hi,

          When are calling this ?
          Do you have the main event loop running ?

          It's being called from the main thread after the worker started. So worker starts and it has 1 default timmer >

                      setObjectName(threadName + "_thread");
                      mTrigger = new QTimer();
                      mTrigger->setInterval(threadInterval);
                      mTrigger->moveToThread(this);
                      connect(mTrigger, &QTimer::timeout, this, &icGenericTimeoutWorker::triggerSignal,Qt::DirectConnection);
                      connect(this, &QThread::started, this, &icGenericTimeoutWorker::startIcTimmer,Qt::DirectConnection);
                      connect(this, &QThread::finished, mTrigger, &QObject::deleteLater,Qt::DirectConnection);
                      connect(this, &QThread::finished, this, &QObject::deleteLater);
          

          And the function in 1st post is where I want to add another timer to the existing worker...

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 6 May 2021, 19:50 last edited by
            #4

            With only bits of your code it's not possible to understand your implementation and the possible issues you might be facing.

            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
            0
            • D Offline
              D Offline
              Dariusz
              wrote on 6 May 2021, 19:58 last edited by
              #5

              @SGaist said in QTimer & Thread, adding it to running thread...:

              With only bits of your code it's not possible to understand your implementation and the possible issues you might be facing.

              Today is special day for my brain and I literally fail at basics... I forgot to start the worker... thus its event loop never run... thus it never started the damn timmer... ehhh!!!!

              Everything works fine once I started the worker... yayyyy I learned something new! :- )

              1 Reply Last reply
              0

              5/5

              6 May 2021, 19:58

              • Login

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