QML(14)——QML与C++交互方式总结1/3(qml调用C++的public函数)

一、效果

qml文件中,可以调用C++类的公共函数

二、步骤

1、C++类文件

创建C++文件时,一定要勾选下面3项

MyQmlClass.h

#ifndef MYQMLCLASS_H
#define MYQMLCLASS_H

#include <QObject>

class MyQmlClass : public QObject {
  Q_OBJECT
  
 public:
  explicit MyQmlClass(QObject *parent = nullptr);

  // 【函数前加上Q_INVOKABLE】
  Q_INVOKABLE void setValue(int value);
  Q_INVOKABLE int getValue();

 private:
  int m_Value;
  
};

#endif  // MYQMLCLASS_H

MyQmlClass.cpp


#include "myqmlclass.h"
#include <QDebug>

MyQmlClass::MyQmlClass(QObject *parent)
    : QObject(parent), m_Value(0){}

void MyQmlClass::setValue(int value) { m_Value = value; }

int MyQmlClass::getValue() {
  qDebug() << "MyQmlClass::getValue";
  return m_Value;
}


2、注册

注册有两种方式

方式1:  engine.rootContext()->setContextProperty("myQml", &myQmlImp);

main.cpp


#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "myqmlclass.h"

int main(int argc, char *argv[]) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

  QGuiApplication app(argc, argv);

  QQmlApplicationEngine engine;

  //【 方式1:创建一个C++对象,注册给qml使用 】
  MyQmlClass myQmlImp;
  engine.rootContext()->setContextProperty("myQml", &myQmlImp);


  const QUrl url(QStringLiteral("qrc:/main.qml"));
  QObject::connect(
      &engine, &QQmlApplicationEngine::objectCreated, &app,
      [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl) QCoreApplication::exit(-1);
      },
      Qt::QueuedConnection);
  engine.load(url);

  return app.exec();
}

方式2:qmlRegisterType<MyQmlClass>("MyClass.module", 1, 0, "MyQml");

main.cpp


#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include "myqmlclass.h"

int main(int argc, char *argv[]) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

  QGuiApplication app(argc, argv);

  QQmlApplicationEngine engine;
  
  //【方式2: 把整个C++类注册到qml,作为qml的一个组件对象】
  qmlRegisterType<MyQmlClass>("MyClass.module", 1, 0, "MyQml");

  const QUrl url(QStringLiteral("qrc:/main.qml"));
  QObject::connect(
      &engine, &QQmlApplicationEngine::objectCreated, &app,
      [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl) QCoreApplication::exit(-1);
      },
      Qt::QueuedConnection);
  engine.load(url);

  return app.exec();
}

3、qml中使用

3.1

如果注册时使用方式1,qml中可以直接使用 注册名.function()

使用这种方式时,IDE不会自动提示相关关键字,但是完成后运行实现了效果。

Register01.qml

import QtQuick 2.0
import QtQuick.Controls 2.15

Item {

    Column {
        anchors.centerIn: parent
        spacing: 10

        Label {
            id: title
            width: 200
            height: 50
            font.pixelSize: 20
            // Column中元素尺寸一定要固定,否则对齐也会失效!
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            text: "方式一"
        }

        Label {
            id: getLabel
            width: 200
            height: 50
            verticalAlignment: Text.AlignVCenter
            text: "label"
        }
        Button {
            id: getBtn
            width: 200
            height: 50
            text: "get"
            onClicked: {
                //【方式1 调用】
                getLabel.text = myQml.getValue()
            }
        }

        TextField {
            id: setLabel
            width: 200
            height: 50
            placeholderText: "enter value"
        }

        Button {
            id: setBtn
            width: getBtn.width
            height: getBtn.height
            text: "set"
            onClicked: {
                //【方式1 调用】
                myQml.setValue(setLabel.text)
            }
        }
    }
}
main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Loader {
        id: contentPane
        anchors.fill: parent
    }

    Component.onCompleted: contentPane.source = "qrc:/Register01.qml"
}

3.2

如果注册时使用方式2,使用C++类就像QML 的组件一样,需要导库,创建,调用

import QtQuick 2.0
import QtQuick.Controls 2.15
// 【方式2 导入库】
import MyClass.module 1.0

Item {

    // 【方式2 创建对象】
    MyQml {
        id: myQmlObj
    }
    Column {
        anchors.centerIn: parent
        spacing: 10

        Label {
            id: title
            width: 200
            height: 50
            font.pixelSize: 20
            // Column中元素尺寸一定要固定,否则对齐也会失效!
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            text: "方式二"
        }

        Label {
            id: getLabel
            width: 200
            height: 50
            verticalAlignment: Text.AlignVCenter
            text: "label"
        }

        Button {
            id: getBtn
            width: 200
            height: 50
            text: "get"
            onClicked: {
                // 【方式2 使用id名调用】
                getLabel.text = myQmlObj.getValue()
            }
        }

        TextField {
            id: setLabel
            width: getLabel.width
            height: getLabel.height
            placeholderText: "enter value"
        }

        Button {
            id: setBtn
            width: getBtn.width
            height: getBtn.height
            text: "set"
            onClicked: {
                myQmlObj.setValue(setLabel.text)
            }
        }
    }
}
main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Loader {
        id: contentPane
        anchors.fill: parent
    }

    Component.onCompleted: contentPane.source = "qrc:/Register01.qml"
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt QML是一种基于JavaScript的声明式语言,用于快速构建跨平台的用户界面。它有时需要与C++代码交互,因为C++可以提供更高性能和更底层的功能。 要在Qt QML调用C函数,需要进行以下步骤: 1. 创建一个继承自QObject的C++类,并在其中定义所需的函数。这些函数需要使用Q_INVOKABLE宏进行标记,以便在QML调用。 ```cpp // MyFunctions.h #include <QObject> class MyFunctions: public QObject { Q_OBJECT public: explicit MyFunctions(QObject *parent = nullptr); Q_INVOKABLE void myFunction(); }; ``` 2. 在QML文件中导入C++类,并使用其实例调用函数。 ```qml import MyFunctions 1.0 Window { // ... Button { text: "调用C函数" onClicked: { MyFunctions.myFunction(); } } // ... } ``` 3. 在C++代码中将该类注册到QML引擎中。 ```cpp // main.cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #include "MyFunctions.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); qmlRegisterType<MyFunctions>("MyFunctions", 1, 0, "MyFunctions"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); } ``` 通过以上步骤,就可以在Qt QML中成功调用C函数了。在按钮点击事件中调用C++类的函数,可以在C++代码中执行所需的操作,并将结果返回到QML界面中进行展示。这种方式可以实现Qt QML框架与C++高性能功能的结合,使得开发者能够更好地发挥Qt的优秀特性和灵活性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值