note: 本篇只是用来说明C++与qml使用信号和槽的连接方式;
C++部分
在main.cpp中将发送信号的类注册进qml 中,qml可以使用; qmlRegisterType<ThreadTrackMeasure>("ThreadTrackMeasure",1,0,"ThreadTrackMeasure"); // ThreadTrackMeasure类文件 class ThreadTrackMeasure : public QThread { Q_OBJECT public: ThreadTrackMeasure(); signals: void measureResult(double x,double y,double z); public slots: void endMeasure(); void startMeasure(); protected: void run() Q_DECL_OVERRIDE; private: bool m_stop = false; Measurement_Gui meausreOperator; };
qml部分
导入文件 import ThreadTrackMeasure 1.0 // 定义 ThreadTrackMeasure { id: threadTrackMeasure } // 在qml中定义槽函数 function onMeasureResult(x,y,z) { ... } ... 在只执行一次的地方进行,信号和槽的关联 Component.onCompleted: { // 连接信号和槽 threadTrackMeasure.measureResult.connect(onMeasureResult) }