欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Qt工作笔记-通过C++使widgets与QQuick交互(包含qml界面对象与C++对象映射)

发布时间:2025/3/15 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Qt工作笔记-通过C++使widgets与QQuick交互(包含qml界面对象与C++对象映射) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

目录

理论及程序运行

源码


 

理论及程序运行

这里要注意,通过qmlRegisterType函数去注册一个QML类!

下面再指明一个关键的问题,如何把QML界面的对象映射到C++呢!

可以有如下的处理:

通过这个函数:

QObject *pRoot = (QObject*)ui->quickWidget->rootObject();

 

还有几种方法:

注册的QML类也是具有QObject和元对象的性质的:

通过设置ObjectName(这个要唯一)

C++映射的时候通过

qDebug() << list[4]->objectName();

即可映射成功!

 

程序运行截图如下:

 

源码

文件结构如下:

源码如下:

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_H

widget.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_H

getmsg.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 = '红色方框内坐标: ' + mouse.x + ',' +mouse.y + '\n'+ 'quickwidgets窗口坐标: ' + + 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的界面是这样的:

新人创作打卡挑战赛发博客就能抽奖!定制产品红包拿不停!

总结

以上是生活随笔为你收集整理的Qt工作笔记-通过C++使widgets与QQuick交互(包含qml界面对象与C++对象映射)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。