qml 与 c++交互

感觉qml,有利有弊,编写业务逻辑,使用起来感觉不爽。


qml界面语言与qt c++交互麻烦。c++到qml都要qmlRegisterType方式,  setContextProperty 方式。

qml到c++,就像解析xml一样,找啊找啊



1,qml调用c++  qmlRegisterType方式

#include "mainwindow.h"
#include <QApplication>
#include <QScrollArea>
#include <QPushButton>
#include <QLabel>
#include <QImage>
#include <QQmlEngine>
#include <QtQml>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickItem>
#include <QMetaObject>
#include <QDebug>
#include <QColor>
#include <QVariant>


int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    qmlRegisterType<ColorMaker>("an.qt.ColorMaker", 1, 0, "ColorMaker");
    QQmlApplicationEngine engine;
    //engine.rootContext()->setContextProperty("colorMaker", new ColorMaker);
    engine.load(QUrl(QStringLiteral("qrc:/cpptoQml.qml")));
    return app.exec();
}


class ColorMaker : public QObject
{
    Q_OBJECT

public:

    int nn = 0;
    Q_INVOKABLE  int setA(int a)
    {
        nn = a;
        return nn;
    }

signals:
    void colorChanged(const QColor & color);

    void currentTime(const QString &strTime);

public slots:
    void start()
    {
        qDebug() << "qqqq" << nn;
    }

    void stop()
    {

    }
};

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.4
import an.qt.ColorMaker 1.0
Window {
    id:rootid
    objectName: "rootid";
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World qt5")
    color: "gray"
    Item {
        ColorMaker {
        id:colorMaker;

        }
        width: 200
        height: 200
        visible: true
        layer.enabled: true
        id: nonLayered
        focus: true
        Keys.enabled: true
        Keys.onPressed:  {
            var component;
            var sprite;
            if(focus == true)
            {
                console.log("mouse button onSpacePressed 1")

                var n = colorMaker.setA(100);
                console.log("mouse button onSpacePressed " + n );
                colorMaker.start();
            }
            else
                console.log("mouse button onSpacePressed")
        }
    }
    Text {
        objectName: "textID";
        id:textID
        x:100
        y:100
        font { pixelSize: 19; bold: true }
        text: "dddddddddddddddd"

    }
}

2,qml调用c++  setContextProperty 

修改这里

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    //qmlRegisterType<ColorMaker>("an.qt.ColorMaker", 1, 0, "ColorMaker");
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("colorMaker", new ColorMaker);
    engine.load(QUrl(QStringLiteral("qrc:/cpptoQml.qml")));

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.4
//import an.qt.ColorMaker 1.0
Window {
    id:rootid
    objectName: "rootid";
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World qt5")
    color: "gray"
    Item {
        /*ColorMaker {
        id:colorMaker;

        }*/
        width: 200
        height: 200
        visible: true

3,c++调用qml

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    //qmlRegisterType<ColorMaker>("an.qt.ColorMaker", 1, 0, "ColorMaker");
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("colorMaker", new ColorMaker);
    engine.load(QUrl(QStringLiteral("qrc:/cpptoQml.qml")));



    QObject * rootItem  = 0;
    QList<QObject*> listQ = engine.rootObjects();
    for(  QList<QObject*>::iterator it = listQ.begin();it != listQ.end();++it)
    {
        qDebug() << " return - " << (*it)->objectName();
        if( (*it)->objectName() == "rootid")
        {
            rootItem = *it;
            break;
        }

    }
    if(!rootItem)
        return 0;
    QObject * textLabel = rootItem->findChild<QObject*>("textID");
    if(textLabel)
    {
        bool bRet = QMetaObject::invokeMethod(textLabel, "setText", Q_ARG(QString, "world hello"));
        qDebug() << "call setText return - " << bRet;
        textLabel->setProperty("color", QColor::fromRgb(255,0,0));
        textLabel->setProperty("text",  QString("world hello"));
        bRet = QMetaObject::invokeMethod(textLabel, "doLayout");
        qDebug() << "call doLayout return - " << bRet;

    }
    return app.exec();
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QMLC++ 交互的过程一般分为以下几个步骤: 1. 定义 C++ 类并注册到 QML 中 你需要在 C++ 中定义一个类,该类封装了你想要实现的功能。同时,你需要使用 `qmlRegisterType` 函数将该类注册到 QML 中,以便在 QML 中使用该类。 例如,你可以定义一个名为 `MyClass` 的类,并将其注册到 QML 中: ```cpp class MyClass : public QObject { Q_OBJECT public: Q_INVOKABLE int myFunction(int arg) { // 实现你的功能 } }; qmlRegisterType<MyClass>("com.example", 1, 0, "MyClass"); ``` 在上面的代码中,`Q_INVOKABLE` 用于声明 `myFunction` 函数可从 QML 中调用,`qmlRegisterType` 函数用于将 `MyClass` 类注册到 QML 中。`"com.example"` 表示注册的命名空间,`1` 和 `0` 表示主版本号和次版本号,`"MyClass"` 是在 QML 中使用的类名。 2. 在 QML 中使用 C++ 类 在 QML 中使用 C++ 类时,你需要使用 `import` 语句导入该类所在的命名空间。然后,你可以通过该命名空间来访问该类。例如: ```qml import com.example 1.0 MyClass { id: myClass } Button { onClicked: { var result = myClass.myFunction(42) // 处理返回值 } } ``` 在上面的代码中,`import` 语句用于导入 `com.example` 命名空间,`MyClass` 用于创建一个 `MyClass` 实例,`id` 属性用于设置实例的标识符,`Button` 用于创建一个按钮,`onClicked` 事件处理程序中调用了 `myFunction` 函数,并处理了它的返回值。 3. 在 C++ 中访问 QML 中的对象 如果你需要从 C++ 中访问 QML 中的对象,你可以使用 `QQuickItem` 类提供的 `findChild` 函数。例如: ```cpp QQuickItem *item = qmlEngine.rootObjects().value(0)->findChild<QQuickItem*>("myItem"); if (item) { // 处理 item 对象 } ``` 在上面的代码中,`qmlEngine.rootObjects()` 函数返回 QML 引擎中所有的根对象,`value(0)` 返回第一个根对象,`findChild` 函数用于查找名为 `"myItem"` 的子对象。 以上就是 QMLC++ 交互的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值