c++ - Using QThread and QTimer to run a methode -
hi have class function run according qtimer
(run every 30ms example )
class testclass { public slots: void dosomthing(); } testclass::testclass() { qtimer *timer = new qtimer(); connect(timer , signal(timeout()) , , slot(dosomthing()); timer->start(30); }
but want dosomthing()
function run in separate thread , that's mean make dosomthing()
function in separate thread , control function using timer (run function in thread every time).
class testclass { public slots: void dosomthing(); } testclass::testclass() { qthread thread = new qthread (); connect(thread , signal(started()) , , slot(dosomthing()); qtimer *timer = new qtimer(); connect(???, signal(?????) , ????, slot(?????); // how can continue code timer->start(30); }
how can ?
class testclass : public qobject { q_object public q_slots: void dosomething(); }; int main(int argv, char** args) { qapplication app(argv, args); qtimer *timer = new qtimer(); // thread testclass test; qthread t; test.movetothread(&t); t.start(); // connections qobject::connect(timer, signal(timeout()), &test, slot(dosomething())); return app.exec(); }
regarding comments, can subclass qthread directly way:
class mythreadedclass : public qthread { mythreadclass(){} void dosomething(){ } void run() { dosomething(); } }; int main() { mythreadclass thread(); thread.start(); // mythreadclass running return 0; }
Comments
Post a Comment