diff --git a/README.md b/README.md index 3e4681e..0d3b748 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/udp_brocast/CMakeLists.txt b/udp_brocast/CMakeLists.txt new file mode 100644 index 0000000..ee4acaa --- /dev/null +++ b/udp_brocast/CMakeLists.txt @@ -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) diff --git a/udp_brocast/src/CMakeLists.txt b/udp_brocast/src/CMakeLists.txt new file mode 100644 index 0000000..42caf14 --- /dev/null +++ b/udp_brocast/src/CMakeLists.txt @@ -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}) + diff --git a/udp_brocast/src/main.cpp b/udp_brocast/src/main.cpp new file mode 100644 index 0000000..253ce92 --- /dev/null +++ b/udp_brocast/src/main.cpp @@ -0,0 +1,16 @@ +#include "udpchooser.h" +#include + +int main(int argc, char *argv[]) { + + QApplication a(argc, argv); + + // IpTest m; + + // TcpTesst m; + // m.show(); + UdpChooser u; + u.show(); + + return a.exec(); +} diff --git a/udp_brocast/src/udpbrocastclientwidget.cpp b/udp_brocast/src/udpbrocastclientwidget.cpp new file mode 100644 index 0000000..931a361 --- /dev/null +++ b/udp_brocast/src/udpbrocastclientwidget.cpp @@ -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); + }); + } +} diff --git a/udp_brocast/src/udpbrocastclientwidget.h b/udp_brocast/src/udpbrocastclientwidget.h new file mode 100644 index 0000000..370d39e --- /dev/null +++ b/udp_brocast/src/udpbrocastclientwidget.h @@ -0,0 +1,28 @@ +#ifndef UDPBROCASTCLIENTWIDGET_H +#define UDPBROCASTCLIENTWIDGET_H + +#include "utilities/udpbrocastclient.h" +#include +#include + +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 diff --git a/udp_brocast/src/udpbrocastclientwidget.ui b/udp_brocast/src/udpbrocastclientwidget.ui new file mode 100644 index 0000000..b013847 --- /dev/null +++ b/udp_brocast/src/udpbrocastclientwidget.ui @@ -0,0 +1,81 @@ + + + UdpBrocastClientWidget + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Recive Brocast + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Text Recieve + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + diff --git a/udp_brocast/src/udpbrocastserverwidget.cpp b/udp_brocast/src/udpbrocastserverwidget.cpp new file mode 100644 index 0000000..eb62361 --- /dev/null +++ b/udp_brocast/src/udpbrocastserverwidget.cpp @@ -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()); + } +} diff --git a/udp_brocast/src/udpbrocastserverwidget.h b/udp_brocast/src/udpbrocastserverwidget.h new file mode 100644 index 0000000..f810804 --- /dev/null +++ b/udp_brocast/src/udpbrocastserverwidget.h @@ -0,0 +1,27 @@ +#ifndef UDPBROCASTSERVERWIDGET_H +#define UDPBROCASTSERVERWIDGET_H + +#include "utilities/udpbrocastserver.h" +#include + +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 diff --git a/udp_brocast/src/udpbrocastserverwidget.ui b/udp_brocast/src/udpbrocastserverwidget.ui new file mode 100644 index 0000000..326227d --- /dev/null +++ b/udp_brocast/src/udpbrocastserverwidget.ui @@ -0,0 +1,81 @@ + + + UdpBrocastServerWidget + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + msg to brocast + + + + + + + + + + Qt::Vertical + + + + 383 + 13 + + + + + + + + Brocast + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + diff --git a/udp_brocast/src/udpchooser.cpp b/udp_brocast/src/udpchooser.cpp new file mode 100644 index 0000000..bf856bb --- /dev/null +++ b/udp_brocast/src/udpchooser.cpp @@ -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); +} diff --git a/udp_brocast/src/udpchooser.h b/udp_brocast/src/udpchooser.h new file mode 100644 index 0000000..415e595 --- /dev/null +++ b/udp_brocast/src/udpchooser.h @@ -0,0 +1,28 @@ +#ifndef UDPCHOOSER_H +#define UDPCHOOSER_H + +#include +#include +#include + +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 diff --git a/udp_brocast/src/udpchooser.ui b/udp_brocast/src/udpchooser.ui new file mode 100644 index 0000000..4981a8a --- /dev/null +++ b/udp_brocast/src/udpchooser.ui @@ -0,0 +1,78 @@ + + + UdpChooser + + + + 0 + 0 + 358 + 475 + + + + Form + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + UDP Brocast Server + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + UDP Brocast Client + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + diff --git a/udp_brocast/src/utilities/CMakeLists.txt b/udp_brocast/src/utilities/CMakeLists.txt new file mode 100644 index 0000000..0f3b56f --- /dev/null +++ b/udp_brocast/src/utilities/CMakeLists.txt @@ -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") + diff --git a/udp_brocast/src/utilities/circlebutton.cpp b/udp_brocast/src/utilities/circlebutton.cpp new file mode 100644 index 0000000..f14b873 --- /dev/null +++ b/udp_brocast/src/utilities/circlebutton.cpp @@ -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); +} + +// diff --git a/udp_brocast/src/utilities/circlebutton.h b/udp_brocast/src/utilities/circlebutton.h new file mode 100644 index 0000000..6ce5b3c --- /dev/null +++ b/udp_brocast/src/utilities/circlebutton.h @@ -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 +#include +#include +#include +#include +#include + +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 diff --git a/udp_brocast/src/utilities/roundbutton.cpp b/udp_brocast/src/utilities/roundbutton.cpp new file mode 100644 index 0000000..f307042 --- /dev/null +++ b/udp_brocast/src/utilities/roundbutton.cpp @@ -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); +} diff --git a/udp_brocast/src/utilities/roundbutton.h b/udp_brocast/src/utilities/roundbutton.h new file mode 100644 index 0000000..1ecaac8 --- /dev/null +++ b/udp_brocast/src/utilities/roundbutton.h @@ -0,0 +1,53 @@ +#ifndef ROUNDBUTTON_H +#define ROUNDBUTTON_H + +#include +#include +#include +#include + +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 diff --git a/udp_brocast/src/utilities/udpbrocastclient.cpp b/udp_brocast/src/utilities/udpbrocastclient.cpp new file mode 100644 index 0000000..bf54cd0 --- /dev/null +++ b/udp_brocast/src/utilities/udpbrocastclient.cpp @@ -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)); + }); +} diff --git a/udp_brocast/src/utilities/udpbrocastclient.h b/udp_brocast/src/utilities/udpbrocastclient.h new file mode 100644 index 0000000..21f36df --- /dev/null +++ b/udp_brocast/src/utilities/udpbrocastclient.h @@ -0,0 +1,21 @@ +#ifndef UDPBROCASTCLIENT_H +#define UDPBROCASTCLIENT_H + +#include +#include +#include +#include + +class UdpBrocastClient : public QObject { + Q_OBJECT +public: + UdpBrocastClient(); + +signals: + void getBrocastMsg(QString msg); + +private: + QUdpSocket *brocast_client; +}; + +#endif // UDPBROCASTCLIENT_H diff --git a/udp_brocast/src/utilities/udpbrocastserver.cpp b/udp_brocast/src/utilities/udpbrocastserver.cpp new file mode 100644 index 0000000..66cfce4 --- /dev/null +++ b/udp_brocast/src/utilities/udpbrocastserver.cpp @@ -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!"; + } +} diff --git a/udp_brocast/src/utilities/udpbrocastserver.h b/udp_brocast/src/utilities/udpbrocastserver.h new file mode 100644 index 0000000..63aed2e --- /dev/null +++ b/udp_brocast/src/utilities/udpbrocastserver.h @@ -0,0 +1,18 @@ +#ifndef UDPBROCASTSERVER_H +#define UDPBROCASTSERVER_H + +#include +#include +#include +#include + +class UdpBrocastServer : public QObject { + Q_OBJECT +public: + UdpBrocastServer(QString msg); + +private: + QUdpSocket *brocast_server; +}; + +#endif // UDPBROCASTSERVER_H diff --git a/udp_brocast/src/utilities/utilities.cpp b/udp_brocast/src/utilities/utilities.cpp new file mode 100644 index 0000000..8ce5b36 --- /dev/null +++ b/udp_brocast/src/utilities/utilities.cpp @@ -0,0 +1,6 @@ +#include "utilities.h" + +Utilites::Utilites(){ + a=233; +} + diff --git a/udp_brocast/src/utilities/utilities.h b/udp_brocast/src/utilities/utilities.h new file mode 100644 index 0000000..78c1ce9 --- /dev/null +++ b/udp_brocast/src/utilities/utilities.h @@ -0,0 +1,11 @@ +#include + +using namespace std; + +class Utilites{ +private: + int a; +public: + Utilites(); + +};