P
P
Pavel K2017-12-21 03:26:56
Qt
Pavel K, 2017-12-21 03:26:56

How to stop a timer from another thread?

Greetings!
I continue to deal with threads ...
In general, there is a class MyClass

MyClass::MyClass(QObject * parent =0) {
   _thread = new QThread(0); //-- поток не должен иметь родителя, что бы он не снёс его ненароком, пока выполняется
    connect(_thread, &QThread::started, this, &MyClass::process); //-- при запуске потока сразу начинаем работу
   moveToThread(_thread);
}

void MyClass::process() 
{
   myTimer = new QTimer(this);
   ....
}

void MyClass::stopTimer() {
   myTimer->stop();
}

Those. the timer, in theory, I create already inside another thread, inside the class with the timer everything is OK - stop / start, etc.
BUT if you try to call stopTimer(); from the main thread, then an error occurs
QObject::killTimer: Timers cannot be stopped from another thread

Um... If you do it through a signal/slot, then everything works as it should, but I see no reason to make additional connections... How can I stop the timer from the main thread without a signal/slot?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ighor July, 2017-12-21
@PavelK

Communication between threads should be only through the signal slot. It is therefore impossible to stop the timer without a signal.
The timer should also be deleted in the thread in which it was created.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question