This commit is contained in:
Alvin Young 2020-03-28 21:05:29 +08:00
parent 00e745fda6
commit 57b0b24e40
24 changed files with 896 additions and 1 deletions

View File

@ -1,2 +1,34 @@
# qt_udp_brocast_test
qt udp brocast test
一个用`QT`写的`UDP`广播通讯的测试项目
# 使用
## 克隆/下载
```shell
git clone https://github.com/impressionyang/qt_udp_brocast_test.git
```
## 进入项目根目录
```shell
cd qt_udp_brocast_test/udp_brocast
```
## 编译
```shell
mkdir build
cd build
cmake ..
make
```
## 运行
```shell
./src/udp_brocast
```
![test](https://impressionyang.gitee.io/imgbed/img/blog/deepinscreen20200328205119.gif)

View File

@ -0,0 +1,14 @@
##########################################################################################
# create by impressionyang
# 2020 01 05
##########################################################################################
#set minimum compile version
cmake_minimum_required(VERSION 3.14)
#set project name
project(udp_brocast)
#add the src subdirectory
add_subdirectory(src)

View File

@ -0,0 +1,55 @@
##########################################################################################
# create by impressionyang
# 2020 01 05
##########################################################################################
#set minimum cmake compile version
cmake_minimum_required(VERSION 3.14)
#include(FindPkgConfig)
#set target app name
set(TARGET_NAME udp_brocast)
#use c++ 17 stander
set(CMAKE_CXX_STANDERD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#include this priject on
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#auto genarate ui.h file
set(CMAKE_AUTOUIC ON)
#auto genarete moc file
set(CMAKE_AUTOMOC ON)
#auto genarate .qrc file on
set(CMAKE_AUTORCC ON)
# set QT type
set(QT Core Gui Widgets Network DBus Sql)
#cmake install
set(CMAKE_INSTALL_PREFIX /usr)
#add package lib
find_package(Qt5 REQUIRED ${QT})
# add a source file subdirectory
add_subdirectory(utilities)
#set all resources files
file(GLOB_RECURSE SOURCES "*.cpp")
file(GLOB_RECURSE HEADERS "*.h")
file(GLOB_RECURSE FORMS "*.ui")
file(GLOB_RECURSE RESOURCES "*.qrc")
#genarate a app
add_executable(${TARGET_NAME} ${SOURCES} ${HEADERS} ${FORMS} ${RESOURCES})
#add QT5 dependes
target_link_libraries(${TARGET_NAME} PUBLIC Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Network Qt5::DBus Qt5::Sql)
#qt5_use_modules(${TARGET_NAME} ${QT})

16
udp_brocast/src/main.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "udpchooser.h"
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
// IpTest m;
// TcpTesst m;
// m.show();
UdpChooser u;
u.show();
return a.exec();
}

View File

@ -0,0 +1,20 @@
#include "udpbrocastclientwidget.h"
#include "ui_udpbrocastclientwidget.h"
UdpBrocastClientWidget::UdpBrocastClientWidget(QWidget *parent)
: QWidget(parent), ui(new Ui::UdpBrocastClientWidget) {
ui->setupUi(this);
lock = 1;
}
UdpBrocastClientWidget::~UdpBrocastClientWidget() { delete ui; }
void UdpBrocastClientWidget::on_pushButton_clicked() {
if (lock) {
client = new UdpBrocastClient();
connect(client, &UdpBrocastClient::getBrocastMsg, this, [=](QString msg) {
// this->ui->text.g
ui->plantext->setPlainText(msg);
});
}
}

View File

@ -0,0 +1,28 @@
#ifndef UDPBROCASTCLIENTWIDGET_H
#define UDPBROCASTCLIENTWIDGET_H
#include "utilities/udpbrocastclient.h"
#include <QString>
#include <QWidget>
namespace Ui {
class UdpBrocastClientWidget;
}
class UdpBrocastClientWidget : public QWidget {
Q_OBJECT
public:
explicit UdpBrocastClientWidget(QWidget *parent = nullptr);
~UdpBrocastClientWidget();
private slots:
void on_pushButton_clicked();
private:
Ui::UdpBrocastClientWidget *ui;
UdpBrocastClient *client;
int lock;
};
#endif // UDPBROCASTCLIENTWIDGET_H

View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>UdpBrocastClientWidget</class>
<widget class="QWidget" name="UdpBrocastClientWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Recive Brocast</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item alignment="Qt::AlignHCenter">
<widget class="QLabel" name="label">
<property name="text">
<string>Text Recieve</string>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="plantext"/>
</item>
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -0,0 +1,17 @@
#include "udpbrocastserverwidget.h"
#include "ui_udpbrocastserverwidget.h"
UdpBrocastServerWidget::UdpBrocastServerWidget(QWidget *parent)
: QWidget(parent), ui(new Ui::UdpBrocastServerWidget) {
ui->setupUi(this);
lock = 1;
}
UdpBrocastServerWidget::~UdpBrocastServerWidget() { delete ui; }
void UdpBrocastServerWidget::on_pushButton_clicked() {
if (!ui->plainTextEdit->toPlainText().isEmpty() && lock) {
server = new UdpBrocastServer(ui->plainTextEdit->toPlainText());
}
}

View File

@ -0,0 +1,27 @@
#ifndef UDPBROCASTSERVERWIDGET_H
#define UDPBROCASTSERVERWIDGET_H
#include "utilities/udpbrocastserver.h"
#include <QWidget>
namespace Ui {
class UdpBrocastServerWidget;
}
class UdpBrocastServerWidget : public QWidget {
Q_OBJECT
public:
explicit UdpBrocastServerWidget(QWidget *parent = nullptr);
~UdpBrocastServerWidget();
private slots:
void on_pushButton_clicked();
private:
Ui::UdpBrocastServerWidget *ui;
UdpBrocastServer *server;
int lock;
};
#endif // UDPBROCASTSERVERWIDGET_H

View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>UdpBrocastServerWidget</class>
<widget class="QWidget" name="UdpBrocastServerWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item alignment="Qt::AlignHCenter|Qt::AlignVCenter">
<widget class="QLabel" name="label">
<property name="text">
<string>msg to brocast</string>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="plainTextEdit"/>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>383</width>
<height>13</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Brocast</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -0,0 +1,21 @@
#include "udpchooser.h"
#include "ui_udpchooser.h"
UdpChooser::UdpChooser(QWidget *parent)
: QWidget(parent), ui(new Ui::UdpChooser) {
ui->setupUi(this);
}
UdpChooser::~UdpChooser() { delete ui; }
void UdpChooser::on_pushButton_clicked() {
UdpBrocastServerWidget *server = new UdpBrocastServerWidget();
server->show();
this->setVisible(false);
}
void UdpChooser::on_pushButton_2_clicked() {
UdpBrocastClientWidget *client = new UdpBrocastClientWidget();
client->show();
this->setVisible(false);
}

View File

@ -0,0 +1,28 @@
#ifndef UDPCHOOSER_H
#define UDPCHOOSER_H
#include <QWidget>
#include <udpbrocastclientwidget.h>
#include <udpbrocastserverwidget.h>
namespace Ui {
class UdpChooser;
}
class UdpChooser : public QWidget {
Q_OBJECT
public:
explicit UdpChooser(QWidget *parent = nullptr);
~UdpChooser();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::UdpChooser *ui;
};
#endif // UDPCHOOSER_H

View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>UdpChooser</class>
<widget class="QWidget" name="UdpChooser">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>358</width>
<height>475</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>UDP Brocast Server</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>UDP Brocast Client</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -0,0 +1,11 @@
##########################################################################################
# create by impressionyang
# 2020 01 05
##########################################################################################
cmake_minimum_required(VERSION 3.14)
file(GLOB_RECURSE SOURCES "*.cpp")
file(GLOB_RECURSE HEADERS "*.h")

View File

@ -0,0 +1,114 @@
#include "circlebutton.h"
/*
**************************************************************
1. @ProjName: graphics_test
2. @Author: impressionyang
3. @Date: 2020-01-03
4. @Brief: File Description
**************************************************************
*/
CircleButton::CircleButton(QWidget *parent) : QWidget(parent) {
setFixedSize(2 * radius + 4, 2 * radius + 4);
//
this->radius = 100;
this->front_color = QColor(70, 184, 255);
this->back_color = QColor(255, 255, 255);
this->hover_color = QColor(71, 95, 255);
this->click_color = QColor(2, 2, 171);
this->text = QString("BUTTON");
color_now = front_color;
this->pen = new QPen();
this->painter = new QPainter();
update();
}
CircleButton::CircleButton(int radius, QString text, QWidget *parent)
: QWidget((parent)) {
setFixedSize(2 * radius + 4, 2 * radius + 4);
this->radius = radius;
this->front_color = QColor(70, 184, 255);
this->back_color = QColor(255, 255, 255);
this->hover_color = QColor(71, 95, 255);
this->click_color = QColor(2, 2, 171);
this->text = text;
color_now = front_color;
this->pen = new QPen();
this->painter = new QPainter();
update();
}
void CircleButton::setColor(QColor front, QColor back, QColor hover,
QColor press) {
this->front_color = front;
this->back_color = back;
this->hover_color = hover;
color_now = front;
click_color = press;
update();
}
void CircleButton::setText(QString text) {
this->text = text;
update();
}
void CircleButton::setRadius(int radius) {
this->radius = radius;
update();
}
void CircleButton::mousePressEvent(QMouseEvent *event) {
emit clicked();
this->color_now = click_color;
update();
}
void CircleButton::mouseReleaseEvent(QMouseEvent *event) {
this->color_now = hover_color;
update();
}
void CircleButton::enterEvent(QEvent *event) {
this->color_now = hover_color;
update();
}
void CircleButton::leaveEvent(QEvent *event) {
this->color_now = front_color;
update();
}
void CircleButton::paintEvent(QPaintEvent *event) {
//开始绘制
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
QPen pen(color_now);
painter.setPen(pen);
QBrush brush(color_now, Qt::SolidPattern);
painter.setBrush(brush);
// drwaw ract
QRect rectf(1, 1, 2 * radius + 1, 2 * radius + 1);
int startangle = 0 * 16;
int endangle = 120 * 16;
// painter.drawPie(rectf, startangle, endangle);
painter.drawEllipse(rectf);
// text
QFont f = QFont("Noto", (2 * radius) / 7, QFont::Bold);
painter.setFont(f);
painter.setFont(f);
pen.setColor(back_color);
painter.setPen(pen);
painter.drawText(0, (2 * radius) / 2 - (2 * radius) / 12, (2 * radius),
(2 * radius) / 6, Qt::AlignCenter, text);
}
//

View File

@ -0,0 +1,73 @@
/*
**************************************************************
1. @ProjName: graphics_test
2. @Author: impressionyang
3. @Date: 2020-01-03
4. @Brief: File Description
**************************************************************
*/
#ifndef CIRCLEBUTTON_H
#define CIRCLEBUTTON_H
#include <QBrush>
#include <QColor>
#include <QPainter>
#include <QPen>
#include <QRectF>
#include <QWidget>
class CircleButton : public QWidget {
Q_OBJECT
public:
//按钮的私有方法
explicit CircleButton(QWidget *parent = nullptr);
explicit CircleButton(int radius, QString text, QWidget *parent = nullptr);
//设置颜色
void setColor(QColor front, QColor back, QColor hover, QColor press);
//设置文字
void setText(QString text);
//设置半径
void setRadius(int radius);
private:
//按钮的属性
//半径
int radius;
//前景色
QColor front_color;
//背景色
QColor back_color;
// hover color
QColor hover_color;
// click color
QColor click_color;
// right now color
QColor color_now;
// inner text
QString text;
// Pen
QPen *pen;
// Painter;
QPainter *painter;
// brush
QBrush *brush;
signals:
//按下的信号
void clicked();
protected:
//鼠标进入事件
void enterEvent(QEvent *event) override;
//鼠标离开事件
void leaveEvent(QEvent *event) override;
//鼠标按下事件
void mousePressEvent(QMouseEvent *event) override;
// release
void mouseReleaseEvent(QMouseEvent *event) override;
//绘制事件
void paintEvent(QPaintEvent *event) override;
};
#endif // CIRCLEBUTTON_H

View File

@ -0,0 +1,65 @@
#include "roundbutton.h"
RoundButton::RoundButton(int width, int height, QString text, QWidget *parent)
: QWidget(parent) {
this->width = width;
this->height = height;
this->text = text;
this->front_color = QColor(70, 184, 255);
this->back_color = QColor(255, 255, 255);
this->hover_color = QColor(71, 95, 255);
this->press_color = QColor(2, 2, 171);
now_color = front_color;
setFixedSize(width, height);
}
void RoundButton::setText(QString text) {
this->text = text;
update();
}
void RoundButton::mousePressEvent(QMouseEvent *event) {
emit clicked();
this->now_color = press_color;
update();
}
void RoundButton::mouseReleaseEvent(QMouseEvent *event) {
this->now_color = hover_color;
update();
}
void RoundButton::enterEvent(QEvent *event) {
this->now_color = hover_color;
update();
}
void RoundButton::leaveEvent(QEvent *event) {
this->now_color = front_color;
update();
}
void RoundButton::paintEvent(QPaintEvent *event) {
QPainter painter(this);
QPen pen(now_color);
painter.setRenderHint(QPainter::Antialiasing, true);
// draw button body
pen.setColor(now_color);
pen.setCapStyle(Qt::RoundCap);
pen.setWidth(this->height - 2);
painter.setPen(pen);
painter.drawLine(height / 2 + 1, height / 2, this->width - (height / 2) - 1,
height / 2);
// draw text
// float progress = abs((value * 100) / (360 * 16));
// QString pre_num = QString::number(progress);
QFont f = QFont("Noto", height / 4, QFont::Bold);
pen.setColor(back_color);
painter.setPen(pen);
painter.setFont(f);
painter.setFont(f);
painter.drawText(height / 4, 2, width - height / 2, height - 4,
Qt::AlignCenter, text);
}

View File

@ -0,0 +1,53 @@
#ifndef ROUNDBUTTON_H
#define ROUNDBUTTON_H
#include <QColor>
#include <QPainter>
#include <QPen>
#include <QWidget>
class RoundButton : public QWidget {
Q_OBJECT
public:
explicit RoundButton(int width, int height, QString text,
QWidget *parent = nullptr);
void setText(QString text);
private:
//圆角按钮属性
//高度
int height;
//宽度
int width;
//前景色
QColor front_color;
//背景色
QColor back_color;
//按下色
QColor press_color;
//浮动色
QColor hover_color;
//当前色
QColor now_color;
//文字
QString text;
signals:
//按下的信号
void clicked();
protected:
//鼠标进入事件
void enterEvent(QEvent *event) override;
//鼠标离开事件
void leaveEvent(QEvent *event) override;
//鼠标按下事件
void mousePressEvent(QMouseEvent *event) override;
// release
void mouseReleaseEvent(QMouseEvent *event) override;
//绘制事件
void paintEvent(QPaintEvent *event) override;
};
#endif // ROUNDBUTTON_H

View File

@ -0,0 +1,14 @@
#include "udpbrocastclient.h"
UdpBrocastClient::UdpBrocastClient() {
brocast_client = new QUdpSocket();
brocast_client->bind(10086, QUdpSocket::ShareAddress);
QObject::connect(brocast_client, &QUdpSocket::readyRead, this, [=]() {
//
QByteArray datagram;
datagram.resize(brocast_client->pendingDatagramSize());
brocast_client->readDatagram(datagram.data(), datagram.size());
emit getBrocastMsg(QString(datagram));
});
}

View File

@ -0,0 +1,21 @@
#ifndef UDPBROCASTCLIENT_H
#define UDPBROCASTCLIENT_H
#include <QDebug>
#include <QObject>
#include <QString>
#include <QtNetwork>
class UdpBrocastClient : public QObject {
Q_OBJECT
public:
UdpBrocastClient();
signals:
void getBrocastMsg(QString msg);
private:
QUdpSocket *brocast_client;
};
#endif // UDPBROCASTCLIENT_H

View File

@ -0,0 +1,11 @@
#include "udpbrocastserver.h"
UdpBrocastServer::UdpBrocastServer(QString msg) {
brocast_server = new QUdpSocket();
if (!msg.isEmpty()) {
brocast_server->writeDatagram(msg.toLocal8Bit(), QHostAddress::Broadcast,
10086);
} else {
qDebug() << "brocast server: msg is empty!";
}
}

View File

@ -0,0 +1,18 @@
#ifndef UDPBROCASTSERVER_H
#define UDPBROCASTSERVER_H
#include <QDebug>
#include <QObject>
#include <QString>
#include <QtNetwork>
class UdpBrocastServer : public QObject {
Q_OBJECT
public:
UdpBrocastServer(QString msg);
private:
QUdpSocket *brocast_server;
};
#endif // UDPBROCASTSERVER_H

View File

@ -0,0 +1,6 @@
#include "utilities.h"
Utilites::Utilites(){
a=233;
}

View File

@ -0,0 +1,11 @@
#include <iostream>
using namespace std;
class Utilites{
private:
int a;
public:
Utilites();
};