QML与C++交互
作者:网络转载 发布时间:[ 2014/12/30 11:33:05 ] 推荐标签:C++ .NET
//==========================================================
//qml.qrc
<RCC>
<qresource prefix="/ui">
<file alias="qml/qml_main">Auto_Order_UI.qml</file>
<file>images/Alert_09.png</file>
<file>images/Alert_10.png</file>
<file>images/Alert_11.png</file>
<file>images/Alert_12.png</file>
<file>images/Alert_13.png</file>
<file>images/Alert_14.png</file>
<file>images/Alert_15.png</file>
<file>images/Alert_16.png</file>
<file>images/carmageddon2.png</file>
<file>images/counter-strike.png</file>
<file>images/X3.png</file>
<file alias="qml/qml_sw">sw.qml</file>
<file>images/button.png</file>
</qresource>
</RCC>
//==========================================================
//main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtDeclarative/QDeclarativeView>
#include <QtDeclarative/QDeclarativeComponent>
#include <QtDeclarative/QDeclarativeItem>
#include <QtDeclarative/QDeclarativeEngine>
#include <QtDeclarative/QDeclarativeContext>
#include "numbercount.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
NumberCount *numberCount = new NumberCount();
QDeclarativeContext *context = view.rootContext();
context->setContextProperty("numberCount",numberCount);//注册C++对象到qml
view.setSource(QUrl("qrc:/ui/qml/qml_sw"));
QObject *rootObject = dynamic_cast<QObject*>(view.rootObject());
QObject::connect(numberCount, SIGNAL(resetData(QVariant)), rootObject, SLOT(resetData(QVariant)));//将C++信号与QML槽关联
view.show();
return a.exec();
}
官方文档:
It is possible to call methods of QObject derived types by either exposing the methods as public slots, or by marking the methods Q_INVOKABLE .
也是说 getTotal()和incre()都能在QML中访问。
运行界面:
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11