Move a class that inherits from QObject with all it objects members to QThread
-
wrote on 26 Oct 2023, 11:57 last edited by
hi
Im trying to understend how to work with qthread and signl-slots of qobject.- how to move class with objects members (timers, qserialport etc) to qthread ? i need to move every object in the class sepertly ?
- how to send signal to thread ?
I want to read serial data in differnt thread , and to be able to start and stop it.
the read is in some time interval for simplisity.for this i want thread with timer , and every time out to read the buffer.
tnx
lior -
hi
Im trying to understend how to work with qthread and signl-slots of qobject.- how to move class with objects members (timers, qserialport etc) to qthread ? i need to move every object in the class sepertly ?
- how to send signal to thread ?
I want to read serial data in differnt thread , and to be able to start and stop it.
the read is in some time interval for simplisity.for this i want thread with timer , and every time out to read the buffer.
tnx
liorwrote on 26 Oct 2023, 12:11 last edited by Pl45m4@Lior said in Move a class that inherits from QObject with all it objects members to QThread:
how to move class with objects members (timers, qserialport etc) to qthread ? i need to move every object in the class sepertly ?
class->moveToThread(thread)
.
Note: If you move aQObject
, allocate its members in a function (likeinit()
) after it got moved.
[Edit: or make every member a direct child of your moved class]
If you create new data on the heap in c'tor for example, it wont be moved too.how to send signal to thread ?
Like you would send a signal to any other
QObject
-
hi
Im trying to understend how to work with qthread and signl-slots of qobject.- how to move class with objects members (timers, qserialport etc) to qthread ? i need to move every object in the class sepertly ?
- how to send signal to thread ?
I want to read serial data in differnt thread , and to be able to start and stop it.
the read is in some time interval for simplisity.for this i want thread with timer , and every time out to read the buffer.
tnx
lior@Lior said in Move a class that inherits from QObject with all it objects members to QThread:
how to move class with objects members (timers, qserialport etc) to qthread ?
You don't move a class to a thread but class instances.
There are several ways:- The easiest one: create the class instance in the thread (in run() method for example).
- Use QThread::moveToThread - in this case all it complex members have to be created after moving to the thread.
- Use worker object approach - preferred solution. See https://wiki.qt.io/QThreads_general_usage (it also shows how to use signals/slots across threads)
-
@Lior said in Move a class that inherits from QObject with all it objects members to QThread:
how to move class with objects members (timers, qserialport etc) to qthread ?
You don't move a class to a thread but class instances.
There are several ways:- The easiest one: create the class instance in the thread (in run() method for example).
- Use QThread::moveToThread - in this case all it complex members have to be created after moving to the thread.
- Use worker object approach - preferred solution. See https://wiki.qt.io/QThreads_general_usage (it also shows how to use signals/slots across threads)
wrote on 26 Oct 2023, 12:36 last edited byThis post is deleted! -
@Lior said in Move a class that inherits from QObject with all it objects members to QThread:
how to move class with objects members (timers, qserialport etc) to qthread ?
You don't move a class to a thread but class instances.
There are several ways:- The easiest one: create the class instance in the thread (in run() method for example).
- Use QThread::moveToThread - in this case all it complex members have to be created after moving to the thread.
- Use worker object approach - preferred solution. See https://wiki.qt.io/QThreads_general_usage (it also shows how to use signals/slots across threads)
wrote on 26 Oct 2023, 12:42 last edited by@jsulm
i know that i move a instance, i didnt explain very well the problem.i can add a sample of code , my main problem is , that i added slot of start to my worker class to activate the timer , i get "object::starttimer: timers cannot be started from another thread warning"
im sending the event from the main window to the worker class.
the worker is act as object when it get the the event and i not fully understend the relation between qthread and signal-slot mechanisem.
and when object is created its owner is the thread it created it.
-
to add what the others said, you don't have to make a init function or create everything after the move.
Just make sure that every QObject based class member has your class as parent. the parent will, when moved to an other thread, take all it's children with him/her
-
@jsulm
i know that i move a instance, i didnt explain very well the problem.i can add a sample of code , my main problem is , that i added slot of start to my worker class to activate the timer , i get "object::starttimer: timers cannot be started from another thread warning"
im sending the event from the main window to the worker class.
the worker is act as object when it get the the event and i not fully understend the relation between qthread and signal-slot mechanisem.
and when object is created its owner is the thread it created it.
@Lior said in Move a class that inherits from QObject with all it objects members to QThread:
im sending the event from the main window to the worker class
How exactly? And make sure the timer in your worker was created in the thread where workes is living (see what @J-Hilk wrote).
Signals and slots work just fine between threads, just don't force DirectConnection (QueuedConnection is used between threads). So, simply connect the signals and slots. -
@jsulm
i know that i move a instance, i didnt explain very well the problem.i can add a sample of code , my main problem is , that i added slot of start to my worker class to activate the timer , i get "object::starttimer: timers cannot be started from another thread warning"
im sending the event from the main window to the worker class.
the worker is act as object when it get the the event and i not fully understend the relation between qthread and signal-slot mechanisem.
and when object is created its owner is the thread it created it.
wrote on 26 Oct 2023, 13:20 last edited by@Lior said in Move a class that inherits from QObject with all it objects members to QThread:
the worker is act as object when it get the the event and i not fully understend the relation between qthread and signal-slot mechanisem.
It's explained in the article linked by @jsulm here as well as in the documentation pages here
and when object is created its owner is the thread it created it.
Therefore you move it and make members a child of the moved object or create them later after your object was moved
1/8