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. Getting QObject::startTimer: timers cannot be started from another thread warning. what im doing wrong?
Forum Updated to NodeBB v4.3 + New Features

Getting QObject::startTimer: timers cannot be started from another thread warning. what im doing wrong?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 15.0k Views 1 Watching
  • 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 last edited by
    #1

    i follow the examples from the Qt sdk , starting timer in the QThread Subclass
    but im keep getting the warring and the thread never start the timer
    here is the code :
    @NotificationThread::NotificationThread(QObject *parent)
    :QThread(parent),
    m_timerInterval(0)
    {
    moveToThread(this);
    }

    NotificationThread::~NotificationThread()
    {
    ;
    }

    void NotificationThread::fire()
    {
    WRITELOG("A::fire called -- currentThread:" + QString::number((int)currentThread()->currentThreadId()));
    QVector<StringPer>* batchVectorResult = new QVector<StringPer>();
    emit UpdateGroupNotifications(batchVectorResult);

    }

    void NotificationThread::run()
    {

      connect(&m_NotificationTimer, SIGNAL(timeout()),
              this,SLOT(fire(),Qt::DirectConnection));
    
      WRITELOG("A::run() worker thread -- currentThread:" + QString::number((int)currentThread()->currentThreadId()));
    

    //SetNotificationTimerFromConf();
    QVariant val(ConfigSettings::getInstance()->ReadFromSettingsReturnVariant(SETTINGS_KEY_NOTIFICATIONTHREAD));
    int interval = val.toInt();
    m_NotificationTimer.setInterval(interval);
    m_NotificationTimer.start();

      QThread::exec&#40;&#41;;
    

    }

    void NotificationThread::SetNotificationTimerFromConf()
    {
    QVariant val(ConfigSettings::getInstance()->ReadFromSettingsReturnVariant(SETTINGS_KEY_NOTIFICATIONTHREAD));
    int interval = val.toInt();
    m_NotificationTimer.setInterval(interval);
    }

    void NotificationThread::UpdateNotificationTimerRT(int timerInterval)
    {
    m_NotificationTimer.setInterval(m_timerInterval);
    }

    void NotificationThread::Execute(const QStringList batchReqList)
    {
    QVector<QString>* batchVectorResult = new QVector<QString>();
    start();
    }

    @

    i start the Thread from the main GUI With Execute( )
    Thanks

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      You are not properly understanding the nature of QThread. QThread manages a thread, but it isn't a thread itself. The timer you create in your subclass lives in the thread that creates the QThread instance (just like QThread itself does). Then, you try to manipulate this timer from the new thread (from within your run() reimplementation). Hence, the warning.

      Read "this wiki entry":http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects for more information.

      1 Reply Last reply
      0
      • U Offline
        U Offline
        umen242
        wrote on last edited by
        #3

        so as if i understand you right , the actual thread context is starting from the run method , that is each action i want to be in new thread most to be in between the run method .
        Thanks allot

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Yes, or you take the approach where you don't subclass QThread, but create a QObject-derived worker object, move that to a vanilla QThread instance, give that worker object a slot to start doing its work, and invoke that slot through a queued method call (either by connecting a signal and emitting that, or by using QMetaObject::invokeMethod with the Qt::QueuedConnection parameter).

          1 Reply Last reply
          0
          • U Offline
            U Offline
            umen242
            wrote on last edited by
            #5

            ok i understand ( i think ) what you saying but where can i find some tutorial or example on the subject do you know?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              I did point you to a wiki page, did I not?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sigrid
                wrote on last edited by
                #7

                This "FAQ":http://developer.qt.nokia.com/faq/answer/why_is_the_warning_qobjectstarttimer_timers_cannot_be_started_from_another_ provides information on the issue as well.

                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