c++ - QThread creation and exchanging data over slots -
this thread header file:
#include <qthread> #include <qstring> #include "typedef.h" class imgprocess : public qthread { q_object public: explicit imgprocess(qobject *parent = 0); void run(); bool stop; imageinfo iinf; signals: void valuechanged(int); void newfile(qstring filename, mycustomstruct* metadata); public slots: private slots: }; q_declare_metatype(mycustomstruct); and code use create 10 threads class (above header file imgprocess.h)
void createthreads() { qregistermetatype<mycustomstruct>("mycustomstruct"); qlist<qthread*> threadlist; (int =0;i<10;i++) { qthread* thread = new qthread; imgprocess *prcthread = new imgprocess(this); prcthread->movetothread(thread); connect(prcthread, signal(valuechanged(int)),this, slot(onvaluechanged(int))); connect(prcthread, signal(newfile(qstring,mycustomstruct*)),this, slot(newfile(qstring,mycustomstruct*))); prcthread->start(); threadlist.push_back(thread); } } the valuchanged signal/slot works well, i'm able send integer threads thread creator class, tried qstring , able send qstring thread creator.
now want exchange mycustomstruct have several int, qstring, char, doubles, struct embedded, etc. doesn't work, tried q_declare_metatype, no luck. missing?
in main thread when try read metadata->datetime(); access denied error.
you should call qregistermetatype() make custom types available non-template based functions, such queued signal , slot connections. class or struct has public default constructor, public copy constructor, , public destructor can registered.
you should call example in creaththreads() function:
qregistermetatype<mycustomstruct>("mycustomstruct"); also should declare mycustomstruct class , prepare public default constructor, public copy constructor, , public destructor it.
Comments
Post a Comment