QML使用C++对象
一、定义QObject子类
Myudp.h
#ifndef MYUDP_H
#define MYUDP_H #include <QObject>
#include <QUdpSocket>
class Myudp : public QObject
{
Q_OBJECT
public:
explicit Myudp(QObject *parent = nullptr);
signals:
void rcvdDataSignal(const QByteArray&);
void sendedSignal(const QString&);//发送成功
public slots:
void initUdpSlot();
void requestSlot();
void sendSlot(const QByteArray&);
private:
QUdpSocket* udpClient = nullptr;
const QString localIp="127.0.0.1";
const quint16 localPort=;
const QString aimIp="127.0.0.1";
const quint16 aimPort=;
}; #endif // MYUDP_H
Myudp.cpp
#include "myudp.h" Myudp::Myudp(QObject *parent) : QObject(parent)
{ } /***********************************************/
// z 函数名称:初始化
// h 函数作用:NULL
// u 函数参数:NULL
// x 函数返回值:NULL
// y 备注:NULL
/***********************************************/
void Myudp::initUdpSlot()
{
if(udpClient == nullptr)
{
udpClient = new QUdpSocket(this);
udpClient->bind(QHostAddress(localIp),localPort);
QObject::connect(udpClient,SIGNAL(readyRead()),this,SLOT(requestSlot()));
}
} /***********************************************/
// z 函数名称:接收数据
// h 函数作用:NULL
// u 函数参数:NULL
// x 函数返回值:NULL
// y 备注:NULL
/***********************************************/
void Myudp::requestSlot()
{
if(udpClient->pendingDatagramSize() == )
{
return;
}
QByteArray ba;
ba.resize(udpClient->pendingDatagramSize());
QHostAddress tempHost("");
quint16 port = ;
udpClient->readDatagram(ba.data(),udpClient->pendingDatagramSize(),&tempHost,&port); emit rcvdDataSignal(ba);
} /**
*函数名:发送槽函数
*函数参数:NULL
*函数作用:NULL
*函数返回值:NULL
*备注:NULL
*/
void Myudp::sendSlot(const QByteArray &info)
{
if(info.size()==udpClient->writeDatagram(info,QHostAddress(aimIp),aimPort))
{
QString str = info.toHex().toUpper();
emit sendedSignal(str);
}
}
二、注册Myudp类,在QML中实例化【注册C++类】
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QThread>
#include "myudp.h"
#include <QQuickView>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); qmlRegisterType<Myudp>("Myudp.module",1,0,"Myudp");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -; return app.exec();
}
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import Myudp.module 1.0
import QtQuick.Controls 2.2
Window {
id: root
visible: true
width:
height:
title: qsTr("Hello World") Myudp{
id:udp
}
Row{
Button {
id: connetBtn
text: qsTr("连接")
onClicked: {
udp.initUdpSlot()
}
} Button {
id: sendBtn
text: qsTr("发送")
onClicked: {
udp.sendSlot("")
}
}
}
}
三、注册Myudp对象,在QML直接使用【设置上下文属性】
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QThread>
#include "myudp.h"
#include <QQuickView>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); Myudp udp; QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("udp",&udp);//注册对象
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -; return app.exec();
}
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
Window {
id: root
visible: true
width:
height:
title: qsTr("Hello World") Row{
Button {
id: connetBtn
text: qsTr("连接")
onClicked: {
udp.initUdpSlot()
}
} Button {
id: sendBtn
text: qsTr("发送")
onClicked: {
udp.sendSlot("")
}
}
}
}
ps:举例使用QTimer
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QThread>
#include "myudp.h"
#include <QQuickView>
#include <QQmlContext>
#include <QTimer>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); qmlRegisterType<QTimer>("QTimer.module",1,0,"Timer"); QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -; return app.exec();
}
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import QTimer.module 1.0
import QtQuick.Controls 2.2
Window {
id: root
visible: true
width:
height:
title: qsTr("")
Rectangle {
id: lamp
width:
height:
radius:
color: "red"
anchors.centerIn: parent
}
Timer {
id:countDown
interval:
property bool isChange: true
onTimeout: {
isChange = !isChange
if(isChange){
lamp.color = "black"
}
else{
lamp.color = "red"
}
}
Component.onCompleted: {
countDown.start()
}
}
}
效果:每隔一秒修改背景色

果然采用GPU渲染:

QML使用C++对象的更多相关文章
- 在 C++ 中使用 QML 对象
看过了如何在 QML 中使用 C++ 类型或对象,现在来看如何在 C++ 中使用 QML 对象. 我们可以使用 QML 对象的信号.槽,访问它们的属性,都没有问题,因为很多 QML 对象对应的类型,原 ...
- Python操作qml对象
1. 如何在python里获得qml里的对象? 1.1 获取根对象 QML: import QtQuick 2.12 import QtQuick.Controls 2.12 ApplicationW ...
- QML与Qt C++ 交互机制探讨与总结
介绍 QML和 C++对象可以通过,signals,slots和 属性修改进行交互.对于一个C++对象,任何数据都可以通过Qt的 Meta-Object System暴露给QML(何总方法,后面介绍) ...
- QML 从入门到放弃 第二卷
第二卷如何更快速的放弃,注重的是C++和QML的交互 <1>记事本.. (1) 先测试下不在QML创建C++对象,仅仅在main.cpp添加一个属性函数供调用. 注意只使用槽函数来做到. ...
- [转载]震惊!QWidget竟然可以嵌入到QML中,QMl窗口句柄竟然是这样获取
背景 记得在初学qml时,就被大佬告知Qml的实现有两种方式“view+item”和“engine+widow”,那么能不能将QWidget嵌入到QML中来呢,我收到的答案是不可以,原因是QML的 ...
- Qt Quick快速入门之qml与C++交互
C++中使用qml对象,直接使用findChild获取qml对象,然后调用setProperty方法设置属性,当然必须在加载qml之后才能使用,不然findChild找不到对象,用法如下. engin ...
- 深入解析QML引擎, 第3部分: 绑定类型
原文 QML Engine Internals, Part 3: Binding Types 译者注:这个解析QML引擎的文章共4篇,分析非常透彻,在国内几乎没有找到类似的分析,为了便于国内的QT/Q ...
- QML与Qt C++ 交互机制探讨与总结(转)
原文转自 https://www.cnblogs.com/aoldman/p/4103510.html 介绍 QML和 C++对象可以通过,signals,slots和 属性修改进行交互.对于一个C+ ...
- Qt Quick 之 QML 与 C++ 混合编程具体解释
Qt Quick 技术的引入.使得你能够高速构建 UI ,具有动画.各种绚丽效果的 UI 都不在话下.但它不是万能的.也有非常多局限性,原来 Qt 的一些技术,比方低阶的网络编程如 QTcpSocke ...
随机推荐
- java.lang.AbstractMethodError: org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.isTypeMockable
[转]https://stackoverflow.com/questions/53539930/java-lang-abstractmethoderror-org-powermock-api-mock ...
- 2019-ACM-ICPC-南京区网络赛-D. Robots-DAG图上概率动态规划
2019-ACM-ICPC-南京区网络赛-D. Robots-DAG图上概率动态规划 [Problem Description] 有向无环图中,有个机器人从\(1\)号节点出发,每天等概率的走到下 ...
- Ubuntu增加swap交换空间的步骤
1.首先用命令free查看系统内 Swap 分区大小. free -m total used free shared buffers cached Mem: 2012 1960 51 0 748 95 ...
- pandas模块的基本用法
一.读取文件 import pandas as pd data = pd.read_csv("F:\\ml\\机器学习\\01\\score.csv") #一般读取的是csv文件, ...
- TG2
1,什么滑动窗口? 2,这人讲的不行还是我的问题.. 3,搞得我都想睡觉了 4,1904,不停扫,输出最高的高度 一个差分的过程,先加上再减去,但是要维护一个最值 什么动态开点(只能用线段树并且内存很 ...
- navigator对象及属性(userAgent)(扩展)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HDU4254 A Famous Game
luogu嘟嘟嘟 这题刚开始特别容易理解错:直接枚举所有\(n + 1\)种情况,然后算哪一种情况合法,再统计答案. 上述思想的问题就在于我们从已知的结果出发,默认这种每一种情况中取出\(q\)个红球 ...
- rdma centos 7.3安装
rdma centos 7.3安装 corasql0人评论7680人阅读2017-05-28 16:29:40 1.安装依赖包 yum install epel-release -y yum ...
- 【HDU4622】Reincarnation
[HDU4622]Reincarnation 一眼似乎不可做,但发现\(strlen(x)\)很小,暴力\(O(n^2)\)预处理每个区间\((l,r)\),查询时\(O(1)\)输出就好了 #inc ...
- 前端代码规范-Javascript
命名规范 ECMAScript 规范中标识符采用驼峰大小写格式,驼峰命名法由小(大)写字母开始,后续每个单词首字母都大写.根据首字母是否大写,分为两种方式: Pascal Case 大驼峰式命名法:首 ...