c++ - How call method from Qt GUI in background worker thread using QThread -
i'm trying add gui in qt code recieving data vrpn server. , need continuously send data server application , call action(method) in interface when receive information.
but have problem endless cycle while (running)
. found solution use qthread recieving data server, can't figure out how use method qt class in worker when recieve data server.
i tried make worker way, i'm not sure, how call method class when recieve data server (or if it's @ possible/or exist better way):
#include "worker.h" #include <iostream> #include "vrpn_analog.h" void vrpn_callback vrpn_analog_callback(void* user_data, vrpn_analogcb analog) { (int = 0; < analog.num_channel; i++) { if (analog.channel[i] > 0) { there want call method nextimage(), have in qt class mainwindow } } } // --- constructor --- worker::worker() { } // --- deconstructor --- worker::~worker() { } // --- process --- // start processing data. void worker::process() { /* flag used stop program execution */ bool running = true; /* vrpn analog object */ vrpn_analog_remote* vrpnanalog; /* binding of vrpn analog callback */ vrpnanalog = new vrpn_analog_remote("openvibe_vrpn_analog@localhost"); vrpnanalog->register_change_handler(null, vrpn_analog_callback); /* main loop of program, each vrpn object must called in order process data */ while (running) { vrpnanalog->mainloop(); } }
i'm newbie in using qt i'll grateful help.
add signal callback
worker , pass pointer register function (which passed user_data
).
void vrpn_callback vrpn_analog_callback(void* user_data, vrpn_analogcb analog) { (int = 0; < analog.num_channel; i++) { if (analog.channel[i] > 0) { worker* worker = std::reinterpret_cast<worker>(user_data); worker ->callback(i, analog.channel[i]); } } } void worker::process() { /* flag used stop program execution */ bool running = true; /* vrpn analog object */ vrpn_analog_remote* vrpnanalog; /* binding of vrpn analog callback */ vrpnanalog = new vrpn_analog_remote("openvibe_vrpn_analog@localhost"); vrpnanalog->register_change_handler(this, vrpn_analog_callback);//add pointer here /* main loop of program, each vrpn object must called in order process data */ while (running) { vrpnanalog->mainloop(); } }
then can connect callback signal of worker whatever want independent main window.
connect(worker, &worker::callback, this, &mainwindow::nextimage);
having said suggest using qtimer set timeout 0 call vrpnanalog->mainloop();
event loop of worker can run once in while.
Comments
Post a Comment