A, defines the subclass 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 function name: initializes // H function operates: NULL // U arguments: NULL // X function return value: NULL // the y-Note: NULL / * ***************************************** **** * / void MyUDP :: initUdpSlot () { IF (UdpClient == nullptr a) { UdpClient = new new on QUdpSocket ( the this ); UdpClient -> the bind (QHostAddress (LocalIP), localPort is); QObject :: Connect (UdpClient, the SIGNAL (the readyRead ()), the this , the SLOT (requestSlot ())); } } / * *************************************** ************************************ * / // Z function name: receiving data // H function action: NULL // U arguments: NULL // X function returns the value: NULL // Y NOTE: NULL / * ************************************************************ ************************************************************ * / void MyUDP :: requestSlot () { IF (udpClient-> pendingDatagramSize () == 0 ) { return ; } the QByteArray BA; ba.resize (UdpClient -> pendingDatagramSize ()); QHostAddress tempHost ( "" ); quint16 Port = 0 ; UdpClient -> readDatagram (ba.data (), udpClient-> pendingDatagramSize (), & tempHost, & Port); EMIT rcvdDataSignal (BA); } / * * * function name: transmitting slot function * function parameter: NULL * function operates: NULL * function return value: NULL * Note: NULL * / void MyUDP :: sendSlot ( const the QByteArray & info) { IF (info.size () == udpClient-> writeDatagram (info, QHostAddress (aimIp), aimPort)) { QString STR = info.toHex().toUpper(); emit sendedSignal(str); } }
Second, the registration Myudp class, instantiated in QML [Embodiment 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") } } } }
Third, the registration Myudp objects directly in 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") } } } }