最近有Qter找我问下关于QML和C++混合编程的问题,问我qmlc++是如何交互的,我总结了三点:
//1.第一种C++与QML交互的方法 //C++部分: //这个方法在主类里面 int main(int argc, char *argv[]) { QApplication a(argc, argv); NdooWebView ndooView(0); ndooView.resize(978,686); ndooView.show(); int i= a.exec(); ProjectManagerApi::getInstance()->stopRun(); return i; } //这个方法在主类NdooWebView里面 void Widget::showMainQML() { QDeclarativeView* noticeViewUi = new QDeclarativeView(0); noticeViewUi->setResizeMode(QDeclarativeView::SizeRootObjectToView); //noticeViewUi->setWindowModality(Qt::ApplicationModal); noticeViewUi->setWindowFlags(Qt::FramelessWindowHint|Qt::Dialog|Qt::WindowStaysOnTopHint); noticeViewUi->setAttribute(Qt::WA_TranslucentBackground); noticeViewUi->setStyleSheet("background:transparent;"); noticeViewUi->setViewportUpdateMode(QGraphicsView::FullViewportUpdate); noticeViewUi->setSource(QUrl("qrc:/qml/main.qml")); QGraphicsObject* noticeViewObj = noticeViewUi->rootObject(); connect(noticeViewObj,SIGNAL(signalMainMoveMent(int,QVariant)),this,SLOT(slotMovenoticeViewer(int,QVariant)));//signalMainMoveMent是qml里面的信号,slotMovenoticeViewer是本地的槽 QMetaObject::invokeMethod(noticeViewUi->rootObject(),"ndooNoticeJsonString", Q_ARG(QVariant,QVariant::fromValue(str)));//这个是C++可以把str值传到qml的ndooNoticeJsonString方法里面 str可以作为ndooNoticeJsonString的参数 } //qml部分: import QtQuick 1.1 Rectangle { id: name width: 280; height: 180 signal signalMainMoveMent(int cmd,variant lst);//可以连接c++中的那个信号槽 property variant lstData: [] property int totalNum: 1 property int currentNum: 1 property int curPage: 1 //这个控件触发一个信号到它的parent InoticeViewTopQml{ id:top anchors.left: parent.left anchors.right: parent.right anchors.top: parent.top onSignalItemMoveMent: { signalMainMoveMent(cmd,lst); } } ListModel{ id:notiModel } ListView{ id:lstView } //就是C++里面的注册的方法 function ndooNoticeJsonString(str) { var tmp = str; //做你想做的一些事情 } } ======================================================>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //2.第2种C++与QML交互的方法 //C++部分 //定义一个接口类:datainterface "datainterface.h" class DataInterface : public QObject { Q_OBJECT Q_PROPERTY(QString strError READ getError WRITE setError) public: explicit DataInterface(QObject *parent = 0); QString getError(){ return strError;} void setError(const QString& error){strError = error;} signals: void signalResponseDataReady(bool error,int cmd,QStringList finalData); void signalNotifyUI(bool error,int cmd,QStringList notifyData); public slots: void slotRequestData(int cmd,QStringList parameterList); void slotResponseNotifyUI(bool error,int cmd,QStringList processResult); //qml notify widget to close //process widget singal void slotPcsamResponseDataReady(bool error,int cmd,QStringList finalData); void slotPcsamNotifyUI(bool error,int cmd,QStringList notifyData); void creatConnection(); void slotdownloadOpenFile(); private: QString strError; bool isConnected; }; "datainterface.cpp" #endif // DATAINTERFACE_H #include "datainterface.h" DataInterface::DataInterface(QObject *parent) : QObject(parent),isConnected(false) { creatConnection(); } void DataInterface::slotRequestData(int cmd, QStringList parameterList) { //Widget为主类,以下类似 Widget::instance()->requestData(cmd,parameterList); //PCSAM::instance()->slotRequestData(cmd); } void DataInterface::slotResponseNotifyUI(bool error, int cmd, QStringList processResult) { Widget::instance()->responseNotifyUI(error,cmd,processResult); } //====================process pcsam signal====================================== void DataInterface::slotPcsamNotifyUI(bool error,int cmd, QStringList processResult) { emit signalNotifyUI(error,cmd,processResult); } void DataInterface::slotPcsamResponseDataReady(bool error, int cmd, QStringList finalData) { emit signalResponseDataReady(error,cmd,finalData); } //=======================creat connection in first request(connection depdent instance)========================== void DataInterface::creatConnection() { connect(Widget::instance(),SIGNAL(signalNotifyUI(bool,int,QStringList)),this,SLOT(slotPcsamNotifyUI(bool,int,QStringList))); connect(Widget::instance(),SIGNAL(signalResponseDataReady(bool,int,QStringList)),this,SLOT(slotPcsamResponseDataReady(bool,int,QStringList))); } //QML部分: import MyDataPool 1.1//这个为main里面注册的接口类 import com.nokia.extras 1.1 //* Item{ //cmd为信号的第一个参数指代哪个命令 DataPool{ id:dataPool onSignalNotifyUI:{ if(!error){ switch(cmd){ case WIFI_GET_MODEL:{ console.log("--------ddddd----"+ notifyData); if(notifyData[1]=="unknown") { dlg.currentDevice = "Android Device" return; } dlg.currentDevice = notifyData[1] }break; } } } onSignalResponseDataReady: { switch(cmd) { case TRADE_MS_REQUEST_MAINUI: { //cmd为信号的第一个参数指代哪个命令 if(finalData[0] == "1") { //connectingTips } else if(finalData[0] == "2") { } }break; } } } } #include "dataInterface.h" #include <QtDeclarative> int main(int argc, char *argv[]) { QApplication a(argc, argv); qmlRegisterType<DataInterface>("MyDataPool",1,1,"DataPool");//main注册接口类 int i= a.exec(); ProjectManagerApi::getInstance()->stopRun(); return i; } //具体使用情况多参考QML与C++交互文档
细看见文档:QML与C++交互实例
另外添加以下官方的说明QML2 C++