4/13/2022»»Wednesday

Qtimer Signal Slot Example

4/13/2022

The QTimer class provides a high-level programming interface for: 59: timers. To use it, create a QTimer, connect its timeout signal: 60: to the appropriate slots, and call start. From then on, it will: 61: emit the timeout signal at constant intervals. 62: 63: Example for a one second (1000 millisecond) timer (from the: 64. The following are 30 code examples for showing how to use PySide.QtCore.SIGNAL. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the.

The QTimer::singleShot is used to call a slot/lambda asynchronously after n ms. The basic syntax is: QTimer::singleShot(myTime, myObject, SLOT(myMethodInMyObject)); with myTime the time in ms, myObject the object which contain the method and myMethodInMyObject the slot to call. So for example if you want to have a timer who write a debug. QTimer example for PyQt5 If an operation is performed periodically in the application, such as periodically detecting the CPU value of the host, then the QTimer timer is needed. When the window’s control receives a Timeout signal, it stops this timer.

Hi,

I cannot figure out how moveToThread is working with QTimer class member.

I have a simple code example of my problem.

Description of the code example:
MyObject1:
Create MyObject2.
Move MyObject2 to a thread using moveToThread.
Connect the started signal from QThread to the MyObject1 slot process.

MyObject2:
Class that inherits from QObject.
Contain a QTimer member.
Contain a process slot.

Running behavior:
In the process slot of MyObject2 I start my QTimer class member and I get:
@QObject::startTimer: timers cannot be started from another thread@

Resolve the problem:
I can get rid of this, by defining my QTimer member as a pointer and create it in the process slot.

Question:
What I do not understand is why in the case that my QTimer class member is not a pointer, my timer is running in a different thread?
moveToThread should make it running in the same thread as MyObject2 thread, no?

Code example:
@#include <QDebug>
#include <QThread>

#include 'mythread1.h'
#include 'mythread2.h'

MyObject1::MyObject1(QObject *parent) :
QObject(parent)
{
qDebug() << 'Constructor object1, THREAD :' << QThread::currentThreadId();

}

#ifndef MYTHREAD1_H
#define MYTHREAD1_H

#include 'mythread2.h'

class MyObject1 : public QObject
{
Q_OBJECT
public:
explicit MyObject1(QObject *parent = 0);

signals:
public slots:
protected:
private:
};

#endif // MYTHREAD1_H

#include <QDebug>
#include <QThread>
#include 'mythread2.h'

Qtimer Update Ui

MyObject2::MyObject2(QObject *parent) :
QObject(parent)
{
qDebug() << 'Constructor object2, THREAD :' << QThread::currentThreadId();
}

void MyObject2::process()
{
qDebug() << 'Object2' << func << 'THREAD :' << QThread::currentThreadId();
m_timer.setInterval(5000);
connect (&m_timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
m_timer.start();
}

Signal

Qtimer Python

void MyObject2::onTimeout()
{
qDebug() << func << 'Object2 THREAD :' << QThread::currentThreadId();
}

#ifndef MYTHREAD2_H
#define MYTHREAD2_H

#include <QTimer>

class MyObject2 : public QObject
{
Q_OBJECT
public:
explicit MyObject2(QObject *parent = 0);

signals:

public slots:
void process(void);
void onTimeout(void);

Qtimer Start

protected:

};

#endif // MYTHREAD2_H@

Qtimer Timeout

Thanks in advance for your help.

Qt Qtimer Example

Julien.