目录

理论及程序运行

源码


理论及程序运行

这里要注意,通过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++对象映射)相关推荐

  1. C++\Python\Qt工作笔记-读取txt文件查找某行是否包含keyWord

    文本内容如下: python源码如下: if __name__ == '__main__':f=open('demo.txt')line=f.readline()while line:if 'keyW ...

  2. Qt工作笔记-发送端发送Json格式的数据包,接收端解析数据包

    原理以及运行 原理是因为QJsonDocument提供了一个函数toJson 可以直接把Json数据转成QByteArray,这样就可以直接发送数据包,然后再按照常规方法解析即可,本源码中含解析内容, ...

  3. Qt工作笔记-Qt元对象系统解析【2合1】

    博文转载地址: https://blog.csdn.net/spwper/article/details/51332187 说Qt信号与槽是一个很好机制,不如说Qt的元对象系统很强大.这也是大家讲Qt ...

  4. Qt工作笔记-对QThread使用的进一步认识(exec及对象在哪个线程创建)

    目录 基本概念 代码与实例 基本概念 这里有个官方例子,如下: class WorkerThread : public QThread{Q_OBJECTvoid run() override {QSt ...

  5. C++|Qt工作笔记-对explicit的认识(Qt中一般情况下为什么会自动加上这个关键字)

    对于explicit这个关键字,网上有很多的博客和资料都,但有些博主写的博文感觉莫名其妙,或者写不到重点. 在此我把我自己对explicit关键字的理解写出来: 网上的某一版本: 关于这一版本,我是感 ...

  6. Qt文档阅读笔记|Qt工作笔记-setupUi官方解析与实例(widgets中界面与业务分离)

    目录 前言 官方解析 模拟界面与业务分离过程 博主栗子 前言 在最开始创建Qt项目的时候,已经默认添加好了,现在来分析下,这么做有什么意义! 官方解析 void QWidget::setupUi(QW ...

  7. Qt工作笔记-列表的分页显示(Qt Widgets框架)

    目录 基本概念 演示及代码 打包下载 基本概念 关键是逻辑,因为数据都存储在数据结构中,如何把数据结构里面的东西显示到界面上,这才是分页的关键!估计稍微有代码量的程序员看到这就知道该怎么做了. 这里以 ...

  8. C/C++|Qt工作笔记-4种方法判断当前对象(类)名或标识(继承发,typeid法,元对象className()法,Q_CLASSINFO法)

    回想起3个月前,刚刚参加工作也做过类似的笔记,但只有2种方法,估计刚毕业没有什么墨水,经过3个月时间又多了2种方法: 这些方法都可用于RTTI 第一个方法是继承发(C++中很推荐用这个,感觉用这个结构 ...

  9. Qt工作笔记-Qt5中中文编码方面的笔记

    目前在使用国内的数据库和实时库接口. 说句实话,国内的东西与国外的东西比,在用户体验和接口调用上比还是存在很大的距离. 个人喜欢用QString去存储数据.个人感觉QTL比STL开发起来要快点 这里有 ...

最新文章

  1. DIV+CSS一行两列布局
  2. python2和python3中的range区别
  3. 日志库 winston 的学习笔记 - logger.info 的实现原理单步调试
  4. 常看网页表单数据_数据收集、整理低效繁琐?WPS表单帮你轻松解决
  5. iPhone 史上最大优惠;摩拜「裁员门」反转;百度网盘缩减空间 | 极客头条
  6. java timer 序列化_Java中的定时器Timer使用示例代码
  7. 安装卸载Windows服务方法(2种方法)
  8. 小球碰撞python代码_Java 实现小球碰撞GUI
  9. mysql中的去除空格函数
  10. 回炉重造之JAVA---枚举
  11. 微软office与WPS Office如此相似,微软为什么不告金山侵权呢?
  12. Win10 平台下, LightGBM GPU 版本的安装
  13. HEVC中变换(Transform)过程中的scaling操作的理解
  14. 深度linux 挂载硬盘,Deepin 深度磁盘挂载
  15. MCEN90008 FLUID DYNAMICS
  16. 5. 第五阶段 测试开发技术 - JAVA
  17. maven dependence 的optional 和 exclusions 标签详解
  18. ARM中断向量表的简单分析
  19. stm32流水灯程序设计实现
  20. 谷歌浏览器:embed引入PDF出现缩略图、工具栏

热门文章

  1. 推荐一个wpfsliverlight的图表控件
  2. 这让全场的chinaakd
  3. 用CComPtr吧,COM接口指针很危险
  4. FreeBSD与Linux的比较
  5. 拦截Windows消息
  6. 对于我这个软妹子来说,为什么python编程课会越学越有趣呢?
  7. 经典面试题(17):以下代码将输出的结果是什么?
  8. ubutnu16.04下Intel Realsense D435驱动的安装和python环境的配置
  9. 腐蚀和膨胀(erode and dilate)
  10. Python ValueError: could not convert string to float: ‘-‘ 解决办法