硬汉嵌入式论坛

 找回密码
 立即注册
查看: 1156|回复: 1
收起左侧

[Qt] qml_dup demo

[复制链接]

31

主题

208

回帖

301

积分

高级会员

积分
301
发表于 2019-12-7 09:26:52 | 显示全部楼层 |阅读模式
本帖最后由 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:ocalHost, 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:ocalHost,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   network  core
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

回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106757
QQ
发表于 2019-12-8 12:38:36 | 显示全部楼层
谢谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-4 17:10 , Processed in 0.150440 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表