Qt工作笔记-通过C++使widgets与QQuick交互(包含qml界面对象与C++对象映射)
生活随笔
收集整理的這篇文章主要介紹了
Qt工作笔记-通过C++使widgets与QQuick交互(包含qml界面对象与C++对象映射)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
理論及程序運(yùn)行
源碼
?
理論及程序運(yùn)行
這里要注意,通過qmlRegisterType函數(shù)去注冊(cè)一個(gè)QML類!
下面再指明一個(gè)關(guān)鍵的問題,如何把QML界面的對(duì)象映射到C++呢!
可以有如下的處理:
通過這個(gè)函數(shù):
QObject *pRoot = (QObject*)ui->quickWidget->rootObject();?
還有幾種方法:
注冊(cè)的QML類也是具有QObject和元對(duì)象的性質(zhì)的:
通過設(shè)置ObjectName(這個(gè)要唯一)
C++映射的時(shí)候通過
qDebug() << list[4]->objectName();即可映射成功!
?
程序運(yùn)行截圖如下:
?
源碼
文件結(jié)構(gòu)如下:
源碼如下:
getmsg.h
#ifndef GETMSG_H #define GETMSG_H#include <QObject>class GetMsg : public QObject {Q_OBJECTQ_PROPERTY(QString text READ text WRITE setText) public:GetMsg(QObject *parent = 0);QString text() const;void setText(const QString &text);signals:void textChanged(QString str);private:QString m_text; };#endif // GETMSG_Hwidget.h
#ifndef WIDGET_H #define WIDGET_H#include <QWidget>class GetMsg;namespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();protected:void getMsgInfo();protected slots:void textChanged(const QString &str);private:Ui::Widget *ui;GetMsg *m_data; };#endif // WIDGET_Hgetmsg.cpp
#include "getmsg.h" #include <QDebug>GetMsg::GetMsg(QObject *parent) : QObject(parent) {m_text = ""; }QString GetMsg::text() const {return m_text; }void GetMsg::setText(const QString &text) {m_text = text;emit textChanged(m_text); }main.cpp
#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }widget.cpp
#include "widget.h" #include "ui_widget.h" #include "getmsg.h"#include <QQmlEngine> #include <QQuickItem> #include <QQmlContext> #include <QMetaObject> #include <QDebug> #include <QDateTime>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);//registartionqmlRegisterType<GetMsg>("GetMsg", 1, 0, "GetMsg");ui->quickWidget->setSource(QUrl("qrc:/main.qml"));this->setWindowTitle("CSDN IT1995");QObject *pRoot = (QObject*)ui->quickWidget->rootObject();const QObjectList list = pRoot->children();m_data = static_cast<GetMsg*>(list[4]);qDebug() << list[4]->metaObject()->className();qDebug() << list[4]->objectName();connect(m_data, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString))); }Widget::~Widget() {delete ui; }void Widget::getMsgInfo() {}void Widget::textChanged(const QString &str) {QString currentData = QDateTime::currentDateTime().toString("hh:mm:ss") + " ";currentData.append(str);ui->textEdit->append(currentData); }main.qml
import QtQuick 2.4 import GetMsg 1.0Item {id: boxRectangle{id: redSquarewidth: 240; height: 120anchors.top: parent.top; anchors.left: parent.left; anchors.margins: 50color: "red"//opacityText{text: "Click"font.pixelSize: 16anchors.centerIn: parent}MouseArea{id: readSquareMouseAreaanchors.fill: parenthoverEnabled: trueproperty string buttonID//Value 'All.Buttons' is eqivalent to://'Qt::LeftButton | Qt::RightButton | Qt::MiddleButton .... | Qt::ExtraButton24'acceptedButtons: Qt.AllButtonsonPressed: {if (mouse.button === Qt.LeftButton)buttonID = 'LeftButton'else if (mouse.button === Qt.RightButton)buttonID = 'RightButton'else if (mouse.button === Qt.MidButton)buttonID = 'MiddleButton'else if (mouse.button === Qt.BackButton)buttonID = 'BackButton'else if (mouse.button === Qt.ForwardButton)buttonID = 'ForwardButton'else if (mouse.button === Qt.TaskButton)buttonID = 'TaskButton'else if (mouse.button === Qt.ExtraButton4)buttonID = 'ExtraButton4'else if (mouse.button === Qt.ExtraButton5)buttonID = 'ExtraButton5'else if (mouse.button === Qt.ExtraButton6)buttonID = 'ExtraButton6'else if (mouse.button === Qt.ExtraButton7)buttonID = 'ExtraButton7'else if (mouse.button === Qt.ExtraButton8)buttonID = 'ExtraButton8'else if (mouse.button === Qt.ExtraButton9)buttonID = 'ExtraButton9'else if (mouse.button === Qt.ExtraButton10)buttonID = 'ExtraButton10'else if (mouse.button === Qt.ExtraButton11)buttonID = 'ExtraButton11'else if (mouse.button === Qt.ExtraButton12)buttonID = 'ExtraButton12'else if (mouse.button === Qt.ExtraButton13)buttonID = 'ExtraButton13'else if (mouse.button === Qt.ExtraButton14)buttonID = 'ExtraButton14'else if (mouse.button === Qt.ExtraButton15)buttonID = 'ExtraButton15'else if (mouse.button === Qt.ExtraButton16)buttonID = 'ExtraButton16'else if (mouse.button === Qt.ExtraButton17)buttonID = 'ExtraButton17'else if (mouse.button === Qt.ExtraButton18)buttonID = 'ExtraButton18'else if (mouse.button === Qt.ExtraButton19)buttonID = 'ExtraButton19'else if (mouse.button === Qt.ExtraButton20)buttonID = 'ExtraButton20'else if (mouse.button === Qt.ExtraButton21)buttonID = 'ExtraButton21'else if (mouse.button === Qt.ExtraButton22)buttonID = 'ExtraButton22'else if (mouse.button === Qt.ExtraButton23)buttonID = 'ExtraButton23'else if (mouse.button === Qt.ExtraButton24)buttonID = 'ExtraButton24'info.text = 'Press (' + buttonID + ' shift = '+ (mouse.modifiers & Qt.ShiftModifier ? 'true' : 'false') + ")"var posInBox = redSquare.mapToItem(box, mouse.x, mouse.y)posInfo.text = '紅色方框內(nèi)坐標(biāo): ' + mouse.x + ',' +mouse.y + '\n'+ 'quickwidgets窗口坐標(biāo): ' + + posInBox.x + ' ,' +posInBox.ydata.text = info.text + '\n' + posInfo.text;}onReleased: {btn.text = 'Released (isClick = ' + mouse.isClick + ' wasHeld = ' + mouse.wasHeld + ')'posInfo.text = ''}onPressAndHold: btn.text = 'Press and hold'onClicked: btn.text = 'Clicked (wasHeld=' + mouse.wasHeld + ')'onDoubleClicked: btn.text = 'Double clicked'}}Text{id: posInfoanchors.bottom: parent.bottomanchors.horizontalCenter: parent.horizontalCenteranchors.margins: 20}Text{id: btnanchors.bottom: posInfo.topanchors.horizontalCenter: parent.horizontalCenteranchors.margins: 20}Text{id: infoanchors.bottom: btn.topanchors.horizontalCenter: parent.horizontalCenteranchors.margins: 20}GetMsg{objectName: 'GetInfo'id: data}}widget.ui的界面是這樣的:
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎(jiǎng)!定制產(chǎn)品紅包拿不停!總結(jié)
以上是生活随笔為你收集整理的Qt工作笔记-通过C++使widgets与QQuick交互(包含qml界面对象与C++对象映射)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt文档阅读笔记-Q_PROPERTY解
- 下一篇: Qt工作笔记-QLineEdit中使用s