'errors whan using QTimer in a multithreading application

I am starting a api rest server in a separate Qthread in my application.

the server shall use a library which is using Q timer in several places.

The compilation is ok. But i have errors in execution. I obtained the following errors in several places:

QThread::start: Failed to create thread (Code d’accès non valide.)

QObject::startTimer: Timers can only be used with threads started with QThread

I can't move Qtimers in the server thread. I need to keep the library code sourse as it is.

is there a solution to face this issue ?

Thanks in advance for your help.



Solution 1:[1]

As far as I understand, your library stuff is located in thread A, and your server is located in thread B, and you want to use your library from server's code.

For this, you'll have to create a separate class C (derived from QObject), which you need to put in thread A. Its purpose then would be to make the whole communication between the library and the server.

This communication should be asynchronous (e.g. via slots/signals). It can also be synchronous (thread B waits C's slot to finish), but it's a bit more complicated and probably not recommended.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1