QML与C++混合编程详解(一)

2015-01-22 21:04:23 · 作者: · 浏览: 16

1QMLC++为什么要混合编程

QML与C++为什么要混合编程,简单来说,就是使用QML高效便捷地构建UI,而C++则用来实现业务逻辑和复杂算法,下面介绍了两者间交互的方法与技巧。

2QML访问C++概述

Qt集成了QML引擎和Qt元对象系统,使得QML很容易从C++中得到扩展,在一定的条件下,QML就可以访问QObject派生类的成员,例如信号、槽函数、枚举类型、属性、成员函数等。

QML访问C++有两个方法:一是在Qt元对象系统中注册C++类,在QML中实例化、访问。二是在C++中实例化并设置为QML上下文属性,在QML中直接使用。与后者相比,前者可以使C++类在QML中作为一个数据类型,例如函数参数类型或属性类型,也可以使用其枚举类型、单例等,功能更强大。

3、如何实现可以被QML访问的C++

C++类要想被QML访问,首先必须满足两个条件:一是派生自QObject类或QObject类的子类,二是使用Q_OBJECT宏。QObject类是所有Qt对象的基类,作为Qt对象模型的核心,提供了信号与槽机制等很多重要特性。Q_OBJECT宏必须在private区(C++默认为private)声明,用来声明信号与槽,使用Qt元对象系统提供的内容,位置一般在语句块首行。下面例子在QtCreator3.1.2中创建,Projects选择QtQuickApplication,工程名为Gemini,Component选择QtQuick2.2,然后在自动生成的文件中添砖加瓦。

信号与槽――

(1)添加头文件Gemini.h

#ifndef GEMINI_H
#define GEMINI_H
// Gemini.h
#include 
  
   
#include 
   
     class Gemini : public QObject { Q_OBJECT signals: void begin(); public slots: void doSomething() { qDebug() << "Gemini::doSomething() called"; } }; #endif // GEMINI_H
   
  

Gemini类中的信号begin()和槽doSomething()都可以被QML访问。槽必须声明为public或protected,信号在C++中使用时要用到emit关键字,但在QML中就是个普通的函数,用法同函数一样,信号处理器形式为on ,Signal首字母大写。信号不支持重载,多个信号的名字相同而参数不同时,能够被识别的只是最后一个信号,与信号的参数无关。

(2)修改main.cpp

// main.cpp
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); qmlRegisterType
      
       ("Union.Lotto.Gemini", 1, 0, "Gemini"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); return app.exec(); }
      
     
    
   
  

这里把Gemini类注册(qmlRegisterType)到了Qt元对象系统,当然也可以先实例化再设置为QML上下文属性,相关内容将在后面详细介绍。

(3)修改main.qml

// main.qml
import QtQuick 2.2
import QtQuick.Window 2.1
import Union.Lotto.Gemini 1.0
Window {
    visible: true
    width: 360; height: 360
    title: "Union Lotto Game"
    color: "white"
    MouseArea {
        anchors.fill: parent
        onClicked: {
            gemini.begin()
        }
    }
    Gemini {
        id: gemini
        onBegin: doSomething()
    }
}

Gemini类注册到Qt元对象系统后,并且在QML文件中导入(import),关键字Gemini就可以在当前QML文件中当作一种QML类型来用了。例子中有个MouseArea,单击鼠标时会发送begin()信号,进而调用doSomething()槽函数。

枚举类型――

(1)修改头文件Gemini.h

#ifndef GEMINI_H
#define GEMINI_H
// Gemini.h
#include 
  
   
#include 
   
     class Gemini : public QObject { Q_OBJECT Q_ENUMS(BALL_COLOR) public: Gemini() : m_ballColor(BALL_COLOR_YELLOW) { qDebug() << "Gemini::Gemini() called"; } enum BALL_COLOR { BALL_COLOR_YELLOW, BALL_COLOR_RED, BALL_COLOR_BLUE, BALL_COLOR_ALL }; signals: void begin(); public slots: void doSomething(BALL_COLOR ballColor) { qDebug() << "Gemini::doSomething() called with" << ballColor; if(ballColor != m_ballColor) { m_ballColor = ballColor; qDebug() << "ball color changed"; } } private: BALL_COLOR m_ballColor; }; #endif // GEMINI_H
   
  

Gemini类中添加了public的BALL_COLOR枚举类型,这个枚举类型要想在QML中使用,就用到了Q_ENUMS()宏。

(2)修改main.qml

// main.qml
import QtQuick 2.2
import QtQuick.Window 2.1
import Union.Lotto.Gemini 1.0
Window {
    visible: true
    width: 360; height: 360
    title: "Union Lotto Game"
    color: "white"
    MouseArea {
        anchors.fill: parent
        onClicked: {
            gemini.begin()
        }
    }
    Gemini {
        id: gemini
        onBegin: doSomething(Gemini.BALL_COLOR_RED)
    }
}

在QML中使用枚举类型的方式是 . ,例如Gemini.BALL_COLOR_RED。

成员函数――

(1)修改头文件Gemini.h

#ifndef GEMINI_H
#define GEMINI_H
// Gemini.h
#include 
  
   
#include 
   
     class Gemini : public QObject { Q_OBJECT Q_ENUMS(BALL_COLOR) public: Gemini() : m_ballColor(BALL_COLOR_YELLOW) { qDebug() << "Gemini::Gemini() called"; } enum BALL_COLOR { BALL_COLOR_YELLOW, BALL_COLOR_RED, BALL_COLOR_BLUE, BALL_COLOR_ALL }; Q_INVOKABLE void stop() { qDebug() << "Gemini::stop() called"; } signals: void begin(); public slots: void doSomething(BALL_COLOR ballColor) { qDebug() << "Gemini::doSomething() called with" << ballColor; if(ballColor != m_ballColor) { m_ballColor = ballColor; qDebug() << "ball color changed"; } } private: BALL_COLOR m_ballColor; }; #endif // GEMINI_H
   
  

Gemini类中添加了成员函数stop(),在QML中访问的前提是public或protected成员函数,且使用Q_INVOKABLE宏,位置在函数返回类型的前面。

(2)修改main.qml

// main.qml
import QtQuick 2.2
import QtQuick.Window 2.1
import Union.Lotto.Gemini 1.0
Window {
    visible: true
    width: 360; height: 360
    title: "Union Lotto Game"
    color: "white"
    MouseArea {
        anchors.fill: parent
        onClicked: {
            gemini.begin()
            gemini.stop()
        }
    }
    Gemini {
        id: gemini
        onBegin: doSomething(Gemini.BALL_COLOR_RED)
    }
}

在QML中访问C++的成