Qt中使用qml的方法

2021-10-05 16:32:12 字數 2355 閱讀 8080

1.2 註冊c++類到qml中

qmlregistertype(「testqt」, 1, 0, 「testcontrol」);

其中testcontrol為c++類的名稱,testqt為qml使用的資料

當qml要使用testcontrol的類時,需要包含import testqt1.0

注意:1.在qt5.12.2中,在<>內,內的首字母需要大寫

2.testcontrol類要整合qobject類

這個若用qt creator建立專案,會自動生成

1.5 c++類中將函式註冊到qml中

(1)註冊c++的函式到qml

q_invokablevoid requestrecord(void);

只要在正常函式前面加了q_invokable 關鍵字即可

使用方法:mytestcontrol.requestrecord()

(2)註冊屬性到qml

q_property(qstring strasr read getasrtxt notify notifyasrtxtchaned)

strasr為qml能使用的屬性

getasrtxt為獲取屬性值的函式

notifyasrtxtchaned為訊號,用於通知什麼時候去呼叫getasrtxt()

使用方法:mytestcontrol.strasr

當檢測到定義的值發生變化後,使用:emit notifyasrtxtchaned(); 即可

1.6 connect的使用

普通connect

connect(&stestcontrolutility::getinstance(),signal(notifyplaytts()),this,slot(onnotifyplaytts()),qt::queuedconnection);

定時器使用:

#include

qtimer m_backtoppagetimer;

m_backtoppagetimer.start(5000);//啟動定時器

connect(&m_backtoppagetimer, signal(timeout()), this, slot(onnotifybacktoppage()),qt::queuedconnection);//關聯槽函式

1.2 註冊c++類到qml中

qmlregistertype(「testqt」, 1, 0, 「testcontrol」);

其中testcontrol為c++類的名稱,testqt為qml使用的資料

當qml要使用testcontrol的類時,需要包含import testqt1.0

注意:1.在qt5.12.2中,在<>內,內的首字母需要大寫

2.testcontrol類要整合qobject類

這個若用qt creator建立專案,會自動生成

1.5 c++類中將函式註冊到qml中

(1)註冊c++的函式到qml

q_invokablevoid requestrecord(void);

只要在正常函式前面加了q_invokable 關鍵字即可

使用方法:mytestcontrol.requestrecord()

(2)註冊屬性到qml

q_property(qstring strasr read getasrtxt notify notifyasrtxtchaned)

strasr為qml能使用的屬性

getasrtxt為獲取屬性值的函式

notifyasrtxtchaned為訊號,用於通知什麼時候去呼叫getasrtxt()

使用方法:mytestcontrol.strasr

當檢測到定義的值發生變化後,使用:emit notifyasrtxtchaned(); 即可

1.6 connect的使用

普通connect

connect(&stestcontrolutility::getinstance(),signal(notifyplaytts()),this,slot(onnotifyplaytts()),qt::queuedconnection);

定時器使用:

#include

qtimer m_backtoppagetimer;

m_backtoppagetimer.start(5000);//啟動定時器

connect(&m_backtoppagetimer, signal(timeout()), this, slot(onnotifybacktoppage()),qt::queuedconnection);//關聯槽函式

Qt中使用qml的方法

1.2 註冊c 類到qml中 qmlregistertype testqt 1,0,testcontrol 其中testcontrol為c 類的名稱,testqt為qml使用的資料 當qml要使用testcontrol的類時,需要包含import testqt1.0 注意 1.在qt5.12.2中,...

在QML中使用QPainter

要想在 qml 中進行繪圖,有很多方法,1 在我之前的部落格中,就講過如何在 qml 中使用 opengl,這是一種方法,但它要求 opengl 的基礎。2 一種方法是使用 qpainter,它的好處是提供很多易用和實用的繪圖 api,而且在很多在 widgets 的使用的 可以直接移到qml中來使...

Qt中使用中文的簡便方法

在qt中使用中文字元的簡便方法 1 首先,在main函式中加入 include qtextcodec setcodecfortr qtextcodec codecforname gb2312 qtextcodec setcodecforlocale qtextcodec codecforname g...