QML与C++交换数据
来源:互联网 发布:淘宝话费流量券大礼包 编辑:程序博客网 时间:2024/06/05 17:28
C++调用QML方法
QML和C++对象之间可以通过信号、槽、属性修改等机制进行通信。对于一个C++对象,任何一个暴露在Qt的元对象系统中的数据–属性–信号–槽和使用Q_INVOKEABLE标记的方法都可以在QML中访问。在QML端,所有QML对象的数据都可以在Qt元对象系统和C++中访问。
//MyItem.qmlimport QtQuick 1.0Item { function myQmlFunction(msg){ console.log("Got message:",msg) return "some return value" }}
对应的cpp
QDeclarativeEngine engine;QDeclarativeComponent component(&engine,"MyItem.qml");QObject *object=component.create();QVariant returnedValue;QVariant msg="Hello from c++";QMetaObject::invokeMethod(object,"myQmlFunction",Q_RETURN_ARG(QVariant),returnedValue),Q_ARG(QVariant,msg));
QML调用C++中类对象
//MyItem.qmlimport QtQuick 1.0Item { width:100; height:100; MouseArea{ anchor.fill:parent onClicked:{ myObject.cppMethod("hello from QML"); myObject.cppSlot(12345) } }}
对应的cpp
class MyClass: public Object{ Q_OBJECT public: Q_INVOKABLE void cppMethod(const QString &msg){ qDenug()<<"Called c++ method with"<<msg; }public slots: void sppSlot(int number){ qDebug()<<"Called with C++ slot with"<<number; }};int main(int argc,char *argv[]){ QApplication app(argc,argv); MyClass myClass; view.rootContext()->setContextProperty("myObject",&myClass); view.setSource(QUrl::fromLocalFile("MyItem.qml")); view.show(); return app.exec(); }
//可以将对象转换成实际类型,这样做的好处是可以在调用函数的时候获得编译器检查。
import QtQuick 2.2Item{ width:100;height:200;}
//在main.cpp中
int main(int argc,char *argc[]){ QGuiApplication app(argc,argv); QQmlEngine engine; QQmlComponent component(&engine,QUrl(QStringLiteral("qrc:///main.qml"))); QObject *object=component.create(); ... ...}
如下方式,可以将对象转换成实际类型,这样做的好处是可以在调用函数的时候获得编译器检查。
普通写法:
object->setProperty(‘width’,500);
QQmlProperty(object,’width’).write(500);
//QQuickItem *item=qobject_cast
QGCQmlWidgetHolder::setContextPropertyObject(const QString &name,QObject * object){_ui.qmlWidget->rootContext()->setContextProperty(name,object);}
阅读全文
0 0
- QML与C++交换数据
- objective-c与js交换数据
- PHP与 后台c交换数据
- C数据交换
- C 数据交换
- Linux中pascal与c的数据交换
- 与Lua交换数据
- JSON -- c语言,数据交换
- C/S交换数据过程
- delphi 与 Excel数据交换
- 与Lua交换自定义数据
- dojo与java交换数据
- linux_ioctl与内核交换数据
- Android与互联网交换数据
- MFC对话框与数据交换
- js与server数据交换
- matlab与C++数据交换
- linux_ioctl与内核交换数据
- HTML初学笔记5
- iOS 直播 —— 推流
- hdu 1160 dp (二维最长上升子序列 记录路径
- python爬虫备忘(2)
- SQL Tuning 基础概述03
- QML与C++交换数据
- FastDFS文件集群服务器搭建
- python 在Windows 下切换工作目录
- LeetCode Algorithms #1 Two Sum
- 正则表达式:检测密码由6-21字母和数字组成
- Android OpenGL ES 投影矩阵的设置
- [笔记分享] [Git] progit学习笔记之分支
- 决策树原理
- html高德地图ip定位之后拖动自定义定位地址