c++ - QT Thread exchange data -
i have class inherits qthread, create several instances of class , makes program multi-thread.
i'm using slots/signals exchange data between threads , main thread (threads creator).
i had this:
void foundnewfile(qstring sourcedrive, qstring path, qstring filename); this working perfect.
now decided share metadata of files, this, have own large struct, did:
void foundnewfile(qstring sourcedrive, qstring path, qstring filename, metadata* meta); this metadata large, contains different data types , have several linked structs. when signals emitted, in main thread, when try do:
meta->datetime->creationhour; i access denied error.
1) doing (without metadata) right or 1 wrong?
2) what's solution?
p.s. tried q_declare_metatype , qregistermetatype together, didn't work.
never implement new slots if inherit qthread. not want. qthread object manages thread, not thread. your qthread-derived objects live in main thread, slots run in main thread (not new thread!)
the correct solution is:
- do not subclass qthread. instantiate qthread object.
- subclass qobject create worker.
- instantiate worker , move new thread, using
qobject::movetothread(). - start qthread.
- now, when use signals , slots, slots run in correct thread.
see official qthread documentation example.
Comments
Post a Comment