
注:官方Sample.cpp在“$SDKdir\LeapSDK\samples”下。
本文将演示如何将LeapMotion的数据通过Qt可视化界面输出。
环境:Qt for Windows VS2012 OpenGL版
OS:Windows 7 SP1 64bit
Qt:5.2.1(Qt Creator 3.0.1)
Compiler:Microsoft Visual C++ Compiler 11.0 amd64
Debugger:None.
1.新建Qt工程QtLeap,将SDK下的include和lib目录复制到工程目录下。修改.pro文件添加include和lib目录。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
INCLUDEPATH += $$PWD/include win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/x64/ -lLeap else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/x64/ -lLeapd INCLUDEPATH += $$PWD/lib/x64 DEPENDPATH += $$PWD/lib/x64 win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/lib/x64/libLeap.a else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/lib/x64/libLeapd.a else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/lib/x64/Leap.lib else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/lib/x64/Leapd.lib win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/x86/ -lLeap else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/x86/ -lLeapd INCLUDEPATH += $$PWD/lib/x86 DEPENDPATH += $$PWD/lib/x86 win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/lib/x86/libLeap.a else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/lib/x86/libLeapd.a else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/lib/x86/Leap.lib else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/lib/x86/Leapd.lib |
2.新建C++ Source File,命名为sample.cpp,将SDK中的sample.cpp复制到新建的呃sample.cpp中。
2.1新建头文件sample.h,将sample.cpp中的类定义剪切到sample.h中。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//sample.h内容 #ifndef SAMPLE_H #define SAMPLE_H #include "Leap.h" #include <QString> using namespace Leap; class SampleListener : public Listener { **************************************** //添加头文件sample.h到sample.cpp中 #include"sample.h" |
2.2将sample.cpp中要在窗体显示的信息转换成QString格式,赋值给一个新建的QString类型的变量。
1 2 3 4 5 6 7 8 9 10 |
#include<QString> QString strOut; *************** //QString("Char") //QString::number(number) strOut=QString("Frame id: " )+QString::number(frame.id()) *************** //将main函数注释掉 /*int main()*** **************/ |
2.3strOut应声明为全局变量
1 2 |
//sample.h extern QString strOut; |
3.main.cpp中添加sample的执行语句
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include "sample.h" ******************************* int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); SampleListener listener; Controller controller; controller.addListener(listener); **************** return a.exec(); } |
4.添加一个QtextEdit文本编辑框用于信息输出
1 2 3 4 |
//main.cpp #include<QTextEdit> ****************** QTextEdit textEdit; |
5.添加一个计时器用于数据的循环获取
5.1在mainwindow.cpp中添加QTimer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
****************** #include<QTimer> #include "sample.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QTimer *timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(getstrOut())); timer->start(100); } ****************** void MainWindow::getstrOut() { ui->textEdit->setText(strOut); } |
5.2在mainwindow.h中添加槽函数getstrOut();
1 2 3 4 5 6 7 8 9 10 11 |
********************* namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { ********************* private slots: void getstrOut(); }; |
6.RUN IT.
如无注明,均为原创。转载请注明: 转载自MITGAI`S THINKING
本文链接地址: Leap Motion Sample Qt Version
本文链接地址: Leap Motion Sample Qt Version
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
如果本文对您生活或工作产生了积极影响,那我非常高兴。
如果您愿意为文章的内容或想法提供支持,欢迎点击下边的捐赠按钮,资助作者创作更多高价值高品质的内容。
如果您愿意为文章的内容或想法提供支持,欢迎点击下边的捐赠按钮,资助作者创作更多高价值高品质的内容。