c++ - How to move class in the same thread -
i'm trying add threading in app write in c++ , qt.
i have class framework , 1 name devicemngr.
the framework.h defined below:
class framework : public qobject { q_object qthread frameworkthread; public: framework();
the framework initialized main. main doing :
qapplication app(argc, argv); qthread frameworkthread; framework *deviceframework = new framework; deviceframework->movetothread(&frameworkthread); qobject::connect(&frameworkthread, signal(finished()), deviceframework, slot(deletelater()));
after, main in main windows , give deviceframework argument.
mainui mywindows(*deviceframework);
mywindows discussing deviceframework using signal/slots.
framework based access android device using class devicemngr , methode.
how possible me add devicemngr in same thread framework.
can in framework.cpp:
framework::framework() { device = new devicemngr(); device->movetothread(&frameworkthread); }
and device manager declared below :
class devicemngr : public qobject { qthread frameworkthread; public: devicemngr(); ~devicemngr();
is method place framework , device manager in frameworkthread ?
thanks sebastien
it possible have devicemngr , framework instances in same thread. this, you'll need keep qthread instance want have in common between them in 1 place (or pass pointer).
do have reason not making devicemngr instance child of framework? documentation qobject says following thread affinity of child of qobject instance:
the qobject::movetothread() function changes thread affinity object , children (the object cannot moved if has parent).
http://doc.qt.io/qt-5/threads-qobject.html
this simplest way of getting both objects on frameworkthread.
main.cpp
int main(int argc, char *argv[]) { qapplication app(argc, argv); qthread frameworkthread; framework *deviceframework = new framework(); deviceframework->movetothread(&frameworkthread); qobject::connect(&frameworkthread, signal(finished()), deviceframework, slot(deletelater())); }
framework.hpp
class framework : public qobject { q_object public: framework(); }
framework.cpp
framework::framework() { device = new devicemngr(this); }
Comments
Post a Comment