问题
after long long search over the internet i did not found any reason able solution to my problem which is that:
i have an application that record the user finger movement on demand, (when the user pressing r its start to record when the user press the s its stop the record,
till now i the application was on the console so it worked perfectly with leap motion, now we have decided to move the application to GUI based application, and we have chosen QT platform, we integrated the GUI with our solution , we have placed two push buttons to replace the key pressing , and connected the events. the real problem started when we wanted to see what was captured by the onFrame call back of the leap listener and we have noticed ever since the Main GUI of the QT is preformed the callbacks of the leap are some how blocked or just dont get cpu time(i dont know the real issue) , we tried several solutions like using a wrapper class that inherit from QObject and start a thread from there but it doesnt seem to effect anything.
here is the main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
LeapClass::QLeapLis * smp = LeapClass::QLeapLis::get_instance();
QThread * th = smp->thread();
//smp->moveToThread(th);
th->start(QThread::HighPriority);
w.show();
return a.exec();
}
here the QLeapLis is the Wrapper class, that has a run method that start the device, but still noting happens, i am very close to give up on QT.
if someone knows how to connect those two we will be grateful
Many Thanks.
回答1:
The code below demonstrates how you should use the Listener
API correctly with Qt.
This code does not require to be run through moc
. I've also checked that it builds and runs with Leap SDK.
#include <QApplication>
#include <QLabel>
#include "leap.h"
class MyListener : public QObject, public Leap::Listener {
public:
virtual void onFrame(const Leap::Controller & ctl) {
Leap::Frame f = ctl.frame();
// This is a hack so that we avoid having to declare a signal and
// use moc generated code.
setObjectName(QString::number(f.id()));
// emits objectNameChanged(QString)
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyListener listener;
Leap::Controller controller;
controller.addListener(listener);
QLabel frameLabel;
frameLabel.setMinimumSize(200, 50);
frameLabel.show();
frameLabel.connect(&listener, SIGNAL(objectNameChanged(QString)),
SLOT(setText(QString)));
int rc = a.exec();
controller.removeListener(listener);
return rc;
}
回答2:
You can avoid the issue by not using the Leap callbacks. You can call Controller::frame() using an animation loop or other convenient looping mechanism to get the latest frame of data. (Each Frame object has an ID so you can tell if you have already processed a frame).
For example, in some environments (Processing, the browser animation loop, Unity, Cinder, etc) there is already an update loop that is called at the application's drawing frame rate. Processing frame data more frequently than this can also be a waste of time. Even if your application isn't already multithreaded, you will have to start dealing with some of the complications that can come with multithreading when you use the LM callback implementation.
According to Kuba Ober, QT widgets don't have an animation or draw loop, so his answer showing the correct way to use the Listener callbacks is the right approach here.
来源:https://stackoverflow.com/questions/22621074/connecting-leap-and-qt-togther