Hp_2018 发表于 2019-12-7 09:26:52

qml_dup demo

本帖最后由 Hp_2018 于 2019-12-7 09:42 编辑

来源于网络,亲试可用。

//--------------------MYUDP_H ------------------------------


#ifndef MYUDP_H
#define MYUDP_H

#include <QObject>
#include <QUdpSocket>

class MyUDP : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QByteArray type READ type WRITE setType NOTIFY typeChanged)
public:
    explicit MyUDP(QObject *parent = 0);
    void SayHello();

    QByteArray type();
    void setType(const QByteArray &t);

signals:
    void typeChanged();

public slots:
    void readyRead();

private:
    QUdpSocket *socket;
    QByteArray _type;
};

#endif


//------------------MyUDP.cpp------------------------------------
#include "myudp.h"
MyUDP::MyUDP(QObject *parent) : QObject(parent)
{
socket = new QUdpSocket(this);
socket->bind(QHostAddress::LocalHost, 5824);
connect(socket,SIGNAL(readyRead()),this, SLOT(readyRead()));
}

// send message in client mode
void MyUDP::SayHello()
{
    QByteArray Data;
    Data.append("--Test Message UDP");
    socket->writeDatagram(Data,QHostAddress::LocalHost,5824);
    // 192.168.0.100
}
// ---------------------main.c-----------------------------------

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QCoreApplication>
#include "myudp.h"

int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    qmlRegisterType<MyUDP>("MyUDP", 1,0, "MyUDP");

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
      return -1;

//   MyUDP client;         //去掉注释 就是   client
   //    client.SayHello();   //去掉注释 就是   client


    return app.exec();
}

//-------------------main.qml-------------------------------


import QtQuick 2.9
import QtQuick.Window 2.2
import MyUDP 1.0

Window {
    visible: true
//color: "black"
    width: 320
    height: 240
    title: qsTr("DUP_Server")


    Text {
      id: txt
      text: qsTr("Hello DUP_Server")
      anchors.centerIn: parent
    }

    MyUDP{
      id: myUdp
      onTypeChanged: {
            txt.text = "Recived Msg:" + type;
            console.log("recived massage is " + type)
      }
    }

}void MyUDP::readyRead()
{
    QByteArray Buffer;
    Buffer.resize(socket->pendingDatagramSize());;

    QHostAddress sender;
    quint16 senderPort;

    socket->readDatagram(Buffer.data(),Buffer.size(), &sender, &senderPort);
    qDebug()<< "Message:" << Buffer;
    setType(Buffer);
}

QByteArray MyUDP::type()
{
    return _type;
}

void MyUDP::setType(const QByteArray &t)
{
    _type = t;
    emit typeChanged();
}


//---------------------------Qml_Dup_server.pro----------------------------------------

QT += quick   networkcore
CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += main.cpp \
    myudp.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
    myudp.h

DISTFILES += \
    android/AndroidManifest.xml \
    android/gradle/wrapper/gradle-wrapper.jar \
    android/gradlew \
    android/res/values/libs.xml \
    android/build.gradle \
    android/gradle/wrapper/gradle-wrapper.properties \
    android/gradlew.bat

ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android

eric2013 发表于 2019-12-8 12:38:36

谢谢分享
页: [1]
查看完整版本: qml_dup demo