"QT" qml and C + + mixed programming detailed __linux

Source: Internet
Author: User
Tags mixed

1 , QML with C + + why to mix programming

QML and C + + Why mixed programming, in simple terms, is to use qml efficient and easy to build the UI, while C + + is used to implement business logic and complex algorithms, the following describes the interaction between the methods and techniques.


2 , QML Access C + + Overview

QT integrates the QML engine and the QT Meta object system, making QML easy to expand from C + +, under certain conditions, QML can access members of Qobject derived classes, such as signals, slot functions, enumeration types, attributes, member functions, and so on.

QML access to C + + has two methods: first, in the Qt Meta Object System registered C + + class, in the QML instantiation, access. The second is to instantiate in C + + and set it to QML context property, which is used directly in QML. Compared with the latter, the former can make C + + classes as a data type in QML, such as function parameter types or attribute types, or use their enumeration types, single cases, and so on to be more powerful.


3 , how to implement can be QML accessed by C + + class

C + + class to be QML access, first must meet two conditions: one is derived from the Qobject class or Qobject class, and the second is to use Q_object macros. The Qobject class is the base class of all Qt objects, and as the core of the Qt object model, it provides many important characteristics, such as signal and slot mechanism. Q_object macros must be declared in private (C + + defaults to private) to declare signals and slots, using the content provided by the QT Meta Object system, where the position is generally the first line of the statement block. The following examples are created in QtCreator3.1.2, projects select Qtquickapplication, project name gemini,component Select QtQuick2.2, and then contribute to the automatically generated file.

signal and groove--

(1) Add header file Gemini.h

#ifndef gemini_h
#define GEMINI_H
//Gemini.h
#include <QObject>
#include <QDebug>
Class Gemini:public QObject
{
    q_object
signals:
    void Begin ();
Public slots:
    void DoSomething () {
        qdebug () << "Gemini::d osomething () called";
    }
};
#endif//Gemini_h

The signal begin () and slot dosomething () in the Gemini class can be accessed by QML. The slot must be declared public or protected, the signal used in C + + to use the Emit keyword, but in qml is a normal function, using the same function, the signal processor in the form of the on<signal>,signal first letter uppercase. The signal does not support overloading, multiple signals have the same name and the parameters are not the same, can be recognized only the last signal, and the parameters of the signal independent.

(2) Modify Main.cpp

Main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <qtqml >
#include <Gemini.h>
int main (int argc, char *argv[])
{
    qguiapplication app (argc, argv);
    Qmlregistertype<gemini> ("Union.Lotto.Gemini", 1, 0, "Gemini");
    Qqmlapplicationengine engine;
    Engine.load (Qurl) (Qstringliteral ("qrc:///main.qml"));
    return app.exec ();
}

Here the Gemini Class registration (Qmlregistertype) to the Qt Meta Object System, of course, can be instantiated and then set to the QML context properties, the relevant content will be described in detail later.

(3) Modify 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 C21/>anchors.fill:parent
        onclicked: {
            gemini.begin ()
        }
    }
    gemini {
        Id:gemini
        Onbegin:dosomething ()
    }
}

After the Gemini class is registered with the QT Meta Object System and imported (import) in the qml file, the keyword Gemini can be used as a QML type in the current QML file. In the example, there is a mousearea, which sends the BEGIN () signal when the mouse is clicked, and then calls the DoSomething () slot function.

enumeration Type--

(1) Modify header file Gemini.h

#ifndef gemini_h
#define GEMINI_H
//Gemini.h
#include <QObject>
#include <QDebug>
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::d osomething () called with " << Ballcolor;
        if (Ballcolor!= m_ballcolor) {
            m_ballcolor = Ballcolor;
            Qdebug () << "ball color Changed";
        }
Private:
    ball_color m_ballcolor;
#endif//Gemini_h

The Ball_color enumeration type of public is added to the Gemini class, and this enumeration type is used in QML to use the Q_enums () macro.

(2) Modify 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)
    }
}

Enumeration types are used in QML in the form of <class_name>.<enum_value>, such as gemini.ball_color_red.

member function--

(1) Modify header file Gemini.h

#ifndef gemini_h
#define GEMINI_H
//Gemini.h
#include <QObject>
#include <QDebug>
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::d osomething () called with " << Ballcolor;
        if (Ballcolor!= m_ballcolor) {
            m_ballcolor = Ballcolor;
            Qdebug () << "ball color Changed";
        }
Private:
    ball_color m_ballcolor;
#endif//Gemini_h

The Gemini class adds a member function stop (), which is accessed in QML by public or protected member functions and uses q_invokable macros, in front of the return type of the function.

(2) Modify 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)
    }
}

A member function that accesses C + + in QML is in the form of <id>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.