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



  • 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



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



  • 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



  • 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).



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



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




Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.