詳解QML?調(diào)用?C++?中的內(nèi)容
先說明一下測試環(huán)境
編譯器:vs2017x64
開發(fā)環(huán)境:Qt5.12
這里主要是總結(jié)一下,怎么在 QML 文件中引用 C ++ 文件里定義的內(nèi)容?
很簡單,我們可以在 C ++ 文件中通過 QML 引擎(QQmlEngine class)的上下文對(duì)象(QQmlContext)調(diào)用方法 setContextProperty 設(shè)置對(duì)應(yīng)的引用即可。詳情看看下面的方法聲明:
void?QQmlContext::setContextProperty(const?QString?&name,?QObject?*value); void?QQmlContext::setContextProperty(const?QString?&name, const?QVariant?&value);
可以看到,既可以設(shè)置 QObject 類型的對(duì)象(指針),也可以設(shè)置 QVariant 兼容的類型數(shù)據(jù)(包括基本類型數(shù)據(jù)等)到 QML 引擎的上下文中。然后在 QML 中就可以通過引用名 name 直接調(diào)用即可。
1. 設(shè)置類型數(shù)據(jù)
// main.cpp
#include <QDateTime>
void main() {
//...
QQmlEngine engine;
QDateTime dateTime = QDateTime::currentDateTime();
engine.rootContext()->setContextProperty("dateTime", &dateTime);
//...
}
以上代碼中直接將 QDateTime 類型的數(shù)據(jù)設(shè)置到引擎上下文中。
Rectangle {
id: window
//...
Text {
text: dateTime
}
}
通過引用名 dateTime 將 C ++ 文件中的數(shù)據(jù)綁定到組件 Text 的 text 屬性上,進(jìn)而顯示出來。
2. 設(shè)置對(duì)象指針
上面是設(shè)置數(shù)據(jù),這里設(shè)置的是 QObject 類型的指針,所以在 QML 里還可以調(diào)用 C ++ 文件中定義的對(duì)象,包括屬性和方法等。
首先,定義一個(gè) QObject 的派生類 ApplicationData,從 QObject 派生是必須的。
// applicationdata.h
#include <QObject>
#include <QDateTime>
#include <QTimer>
class ApplicationData : public QObject
{
Q_OBJECT
public:
ApplicationData(){
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &ApplicationData::slt_timeout);
timer->start(1000);
}
Q_INVOKABLE QDateTime getCurrentDateTime() const {
return m_currentDateTime;
}
signals:
void sig_dataTimeUpdated();
private slots:
void slt_timeout() {
m_currentDateTime = QDateTime::currentDateTime();
emit sig_dataTimeUpdated();
}
private:
QDateTime m_currentDateTime;
};
其中 Q_INVOKABLE 用于聲明此方法可被元對(duì)象系統(tǒng)調(diào)用。這個(gè)類實(shí)現(xiàn)每 1000 ms 刷新內(nèi)部日期時(shí)間屬性,并且發(fā)射信號(hào) sig_dataTimeUpdated,此屬性值可以通過調(diào)用定義的公共方法 getCurrentDateTime() 得到。
下面再來定義程序入口文件:
// main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "applicationdata.h"
int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
ApplicationData data;
engine.rootContext()->setContextProperty("currentDateTime", &data);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
在 QML 引擎裝載 QML 文件前,先將類 ApplicationData 的對(duì)象指針設(shè)置到上下文中。
下面再看看怎么調(diào)用指針對(duì)應(yīng)的類對(duì)象。
// main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.VirtualKeyboard 2.4
Window {
id: window
visible: true
title: qsTr("Hello World")
Text {
id: name_id
anchors.centerIn: parent
}
Connections {
target: currentDateTime
onSig_dataTimeUpdated: {
name_id.text = currentDateTime.getCurrentDateTime();
}
}
}
使用 Connections 連接數(shù)據(jù)對(duì)象 currentDateTime 的信號(hào),當(dāng)指針對(duì)象的信號(hào) sig_dataTimeUpdated 發(fā)射出來時(shí),調(diào)用方法 getCurrentDateTime() 并用結(jié)果設(shè)置組件 Text 的屬性 text。
顯示的效果是動(dòng)態(tài)刷新時(shí)間日期數(shù)據(jù)的,這和在上下文中設(shè)置類型數(shù)據(jù)不同(不會(huì)刷新),如下圖:

到此這篇關(guān)于QML 怎么調(diào)用 C++ 中的內(nèi)容?的文章就介紹到這了,更多相關(guān)QML調(diào)用 C++內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言中動(dòng)態(tài)內(nèi)存分配malloc、calloc和realloc函數(shù)解析
C語言跟內(nèi)存申請(qǐng)相關(guān)的函數(shù)主要有 alloca、calloc、malloc、free、realloc等,下面這篇文章主要給大家介紹了關(guān)于C語言中動(dòng)態(tài)內(nèi)存分配malloc、calloc和realloc函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-03-03
C語言靜態(tài)版通訊錄的設(shè)計(jì)與實(shí)現(xiàn)
靜態(tài)版通訊錄是一種簡單的通訊錄實(shí)現(xiàn)方式,通過定義固定的數(shù)組大小來存儲(chǔ)聯(lián)系人信息。該方法不支持動(dòng)態(tài)增刪聯(lián)系人,但具有實(shí)現(xiàn)簡單、易于理解的優(yōu)點(diǎn)。在程序設(shè)計(jì)中,需注意數(shù)組邊界溢出等問題2023-04-04
C++ for循環(huán)與nullptr的小知識(shí)點(diǎn)分享
這篇文章主要是來和大家介紹一些C++中的小知識(shí)點(diǎn),本文分享的是for循環(huán)與nullptr,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-05-05

