标签:ddr hdpi 成功 cal ica quick ext att ons
一、定义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=8080; const QString aimIp="127.0.0.1"; const quint16 aimPort=8888; }; #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() == 0) { return; } QByteArray ba; ba.resize(udpClient->pendingDatagramSize()); QHostAddress tempHost(""); quint16 port = 0; 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中实例化【方式1】
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 -1; 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: 640 height: 480 title: qsTr("Hello World") Myudp{ id:udp } Row{ Button { id: connetBtn text: qsTr("连接") onClicked: { udp.initUdpSlot() } } Button { id: sendBtn text: qsTr("发送") onClicked: { udp.sendSlot("123") } } } }
三、注册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 -1; 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: 640 height: 480 title: qsTr("Hello World") Row{ Button { id: connetBtn text: qsTr("连接") onClicked: { udp.initUdpSlot() } } Button { id: sendBtn text: qsTr("发送") onClicked: { udp.sendSlot("123") } } } }
标签:ddr hdpi 成功 cal ica quick ext att ons
原文地址:https://www.cnblogs.com/judes/p/11241576.html