1.设计一个简单的界面
如下图所示
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>258</width>
<height>446</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>MyPort:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="myPort"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>ToPort:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="toPort"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>ToIp:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="toIp"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_2" native="true">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>聊天记录:</string>
</property>
</widget>
</item>
<item>
<widget class="QTextEdit" name="record"/>
</item>
<item>
<widget class="QWidget" name="widget_3" native="true">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>发送窗口:</string>
</property>
</widget>
</item>
<item>
<widget class="QTextEdit" name="sendEdit"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_4" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="sendBtn">
<property name="text">
<string>发送</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
2.在pro文件中加入 network 模块
3.在widget.h 中声明一个QUdpSocket变量
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QUdpSocket>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
QUdpSocket *udp;
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
4.在widget.cpp 写发送和接收数据的代码
#include "widget.h"
#include "ui_widget.h"
#include <QUdpSocket>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
udp = new QUdpSocket(this);
ui->myPort->setText("8888"); // 这个窗口的端口号
ui->toPort->setText("9999"); // 要发送的窗口的端口号
ui->toIp->setText("127.0.0.1"); // 要发送的端口的IP地址
udp->bind(ui->myPort->text().toInt()); // 绑定自身的端口号
// 给发送按钮设置信号和槽
connect(ui->sendBtn, &QPushButton::clicked, [=](){
udp->writeDatagram(ui->sendEdit->toPlainText().toUtf8(), QHostAddress(ui->toIp->text()),
ui->toPort->text().toInt()); // 发送数据
// 往聊天窗口中添加信息
ui->record->append("my say:" + ui->sendEdit->toPlainText());
ui->record->setAlignment(Qt::AlignRight);
// 清空输入框
ui->sendEdit->clear();
});
// 接收数据
connect(udp, &QUdpSocket::readyRead, [=](){
qint64 size = udp->pendingDatagramSize();
// 读取报文
QByteArray array = QByteArray(size, 0);
udp->readDatagram(array.data(), size);
// 同步到聊天记录中
ui->record->append("he say: " + array);
ui->record->setAlignment(Qt::AlignLeft);
});
}
Widget::~Widget()
{
delete ui;
}
效果如下图所示:
5.按照上面的步骤再写一个窗口
最终效果如图所示: