李程站 发表于 2013-2-4 03:03:21

详解QML与C++混合编程使用

本文适合于对Qt Quick有基本了解的读者。首先回答一个比较常会被问到的问题:什么是QML,它与Quick的关系是什么?
Qt Quick是Qt User Interface Creation Kit的缩写,而QML是Qt Quick最重要的组成部分,Qt Quick结合了如下技术:
组件集合,其中大部分是关于图形界面的
基于JavaScript陈述性语言:QML (Qt Meta-Object Language的缩写)
用于管理组件并与组件交互的C++API -QtDeclarative模块
言归正传:通过Qt Creator,我们可以轻松生成一个Qt Quick的应用工程,从而为QML生成应用程序框架。具体操作详见:创建qt quick (qml) 应用程序。
C++与QML的交互是通过注册C++对象给QML环境得以实现的:
在C++实现中,非可视化的型别均为QObject的子类,可视化的类型均为QDeclarativeItem的子类。注意:QDeclarativeItem等同于QML的Item类。
如果用户想要定义自己的型别,做法如下:
在C++中,实现派生于QObject或QDeclarativeItem的子类,它是新定义item的实体对象;
在C++中,将1中实现的新item类型注册给QML;
在QML中,导入含有1中定义的新item的模块;
在QML中,向使用标准的item一样使用新定义的item
现举例说明,我们现尝试使用用Qt C++实现的MyButton对象(如下qml代码),它有自己的属性、方法以及信号的handler。用法如下(它与使用其它标准的QML item一样),所需要做的是 需要导入包含MyButton的对应模块名称及其版本“MyItems 1.0 ”。

[*]//main.qml   
[*]import Qt 4.7   
[*]import MyItems 1.0   
[*]Item {   
[*]    width: 300; height: 200   
[*]    MyButton {   
[*]      //注意:x, y, width, height是继承自item的属性,无需再自定义的item中实现   
[*]      x: 50; y: 50   
[*]      width: 200; height: 100   
[*]      color: "gray"   //自定义属性   
[*]      onMySignals:dosth//自定义信号mySignals   
[*]MouseArea {   
[*]anchors.fill: parent   
[*]onClicked: parent.myColor()   // 调用C++定义的方法myColor   
[*]}   
[*]    }   
[*]}   
[*]//main.qml
[*]import Qt 4.7
[*]import MyItems 1.0
[*]Item {
[*]    width: 300; height: 200
[*]    MyButton {
[*]      //注意:x, y, width, height是继承自item的属性,无需再自定义的item中实现
[*]      x: 50; y: 50
[*]      width: 200; height: 100
[*]      color: "gray"   //自定义属性
[*]      onMySignals:dosth//自定义信号mySignals
[*]MouseArea {
[*]anchors.fill: parent
[*]onClicked: parent.myColor()   // 调用C++定义的方法myColor
[*]}
[*]    }
[*]}
为了能够上述qml代码工作,需要为在Qt C++代码中注册MyButton及其所属的模块,对应的main.cpp代码如下:

[*]#include <QtGui/QApplication>   
[*]#include "qmlapplicationviewer.h"   
[*]int main(int argc, char *argv[])   
[*]{   
[*]    QApplication app(argc, argv);   
[*]    QmlApplicationViewer viewer;   
[*]    // MyButtonItem是与QML中MyButton相对应的C++实现的类名称   
[*]    // 1,0是版本信息;MyItems是MyButton所属的模块名称   
[*]    qmlRegisterType<MyButtonItem>("MyItems", 1, 0, "MyButton ");   
[*]    viewer.setOrientation(QmlApplicationViewer::Auto);   
[*]    viewer.setMainQmlFile(QLatin1String("qml/untitled/main.qml"));   
[*]    viewer.show();   
[*]    return app.exec();   
[*]}   
[*]#include <QtGui/QApplication>
[*]#include "qmlapplicationviewer.h"
[*]int main(int argc, char *argv[])
[*]{
[*]    QApplication app(argc, argv);
[*]    QmlApplicationViewer viewer;
[*]    // MyButtonItem是与QML中MyButton相对应的C++实现的类名称
[*]    // 1,0是版本信息;MyItems是MyButton所属的模块名称
[*]    qmlRegisterType<MyButtonItem>("MyItems", 1, 0, "MyButton ");
[*]    viewer.setOrientation(QmlApplicationViewer::Auto);
[*]    viewer.setMainQmlFile(QLatin1String("qml/untitled/main.qml"));
[*]    viewer.show();
[*]    return app.exec();
[*]}
上面我们在QML中MyButton对象,有自己的属性、方法以及信号的handler,其实现均来自Qt C++。Qt C++需要作以下工作:首先要定义 QML中MyButton相对应的C++实现MyButtonItem,它必须继承自QDeclarativeItem
为了让MyButton对象能够使用其Color属性,MyButtonItem类需要利用QT的PROPERTY系统,为Moc声明其属性
为了让MyButton对象能够使用其myColor方法,MyButtonItem类需要声明该方法,并标记为Q_INVOKABLE (另外一种解决方案是将myColor声明为槽。
为了让MyButton对象能够接受到C++所emit的信号,并在onMySignals,MyButtonItem类需要声明mySignals信号

[*]class MyButtonItem : public QDeclarativeItem   
[*]{   
[*]    Q_OBJECT   
[*]    Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)   
[*]signals:   
[*]    void colorChanged();   
[*]    void mySignals();   
[*]public:   
[*]    MyButtonItem(QDeclarativeItem *parent = 0);   
[*]    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,   
[*]               QWidget *widget = 0);   
[*]public:   
[*]    const QColor &color() const;   
[*]    void setColor(const QColor &newColor);   
[*]    Q_INVOKABLE QColor myColor() const;   
[*]// Alternatives for myColor to be called from QML   
[*]//public slots   
[*]    //QColor myColor() const;   
[*]private:   
[*]    QColor m_color;   
[*]};
原始作者地址http://blog.csdn.net/changsheng230
小结:关于详解QML与C++混合编程使用的内容介绍完了,希望本文对你有所帮助!
【编辑推荐】

[*]剖析Qt Quick之QML程序
[*]Qt Quick初体验QML编程
[*]详解 Qt Quick 开始使用QML编程
[*]Qt Creator中添加菜单栏 实例
[*]经典分享 C++内存管理详解
[*]深度解析 Qt Quick 宏
页: [1]
查看完整版本: 详解QML与C++混合编程使用