Reputation: 1373
I tried to use QThread
for my first time and I want to emit signal from non-member static function.
My DataReceiver.h
file:
#ifndef DATARECEIVER_H
#define DATARECEIVER_H
#include <QObject>
#include "vrpn_Analog.h"
class DataReceiver : public QObject {
Q_OBJECT
public:
DataReceiver();
public slots:
void check();
signals:
void blink();
};
void VRPN_CALLBACK handle_analog( void* userData, const vrpn_ANALOGCB a );
#endif // DATARECEIVER_H
My DataReceiver.cpp
file:
#include "datareceiver.h"
#include "vrpn_Analog.h"
DataReceiver::DataReceiver()
{
}
void DataReceiver::check()
{
bool running = true;
/* VRPN Analog object */
vrpn_Analog_Remote* VRPNAnalog;
/* Binding of the VRPN Analog to a callback */
VRPNAnalog = new vrpn_Analog_Remote("openvibe_vrpn_analog@localhost");
VRPNAnalog->register_change_handler(NULL, handle_analog);
/* The main loop of the program, each VRPN object must be called in order to process data */
while (running)
{
VRPNAnalog->mainloop();
}
}
void VRPN_CALLBACK handle_analog( void* userData, const vrpn_ANALOGCB a )
{
for( int i=0; i < a.num_channel; i++ )
{
if (a.channel[i] > 0)
{
emit blink();
}
}
}
In handle_analog
I try to emit signal, which I want to use in another class.
void MainWindow::checkChannels()
{
QThread *thread = new QThread;
DataReceiver *dataReceiver = new DataReceiver();
dataReceiver->moveToThread(thread);
//connect(thread, SIGNAL(blink()), this, SLOT(nextImage()));
thread->start();
}
but when I try to run I get error:
error: C2352: 'DataReceiver::blink' : illegal call of non-static member function
I know, where my mistake is, but I don't know, how to fix it.
Upvotes: 1
Views: 1151
Reputation: 7985
It doesn't make sense to emit a signal without a corresponding object to emit it from, since you wouldn't have anything to connect it to.
So what you want is to pass in a pointer to your DataReceiver
as userData
, and implement a public method which emits the signal. Then you can cast userData
to DataReceiver
and call the method on it.
The following incomplete code attempts to show what I mean
void DataReceiver::emitBlink() { // Should of course also be added in header.
emit blink();
}
...
/// Pass in "this" as userData
VRPNAnalog->register_change_handler(this, handle_analog);
...
void VRPN_CALLBACK handle_analog( void* userData, const vrpn_ANALOGCB a )
{
...
reinterpret_cast<DataReceiver*>(userData)->emitBlink();
...
}
Upvotes: 1