实现效果
实现功能
创建两个界面,可以通过udp进行通信,并显示通信内容。 界面部分由代码实现,并使用qss简单美化。 udp通信由创建套接字,绑定端口号,发送和接收数据函数完成。
代码实现
创建第一个通信对象 ud1.h
#ifndef UDPUSER
#define UDPUSER
#include <QMainWindow>
#include <QLabel>
#include <QPushButton>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QList>
#include <QLineEdit>
#include <QGridLayout>
#include <QUdpSocket>
class udpUser : public QMainWindow
{
Q_OBJECT
public:
udpUser(QWidget *parent = nullptr);
~udpUser();
void layOut();
private:
QLabel* myLabel[5];
QPushButton *sendButton;
QTextEdit *recordText;
QTextEdit *textEdit;
QVBoxLayout *vLayOut;
QList<QString> labelName;
QList<QString> labelNum;
QLineEdit *myLine[3];
QGridLayout *gridLayout;
QUdpSocket *udpSocket;
};
#endif
ud1.cpp
#include "udp1.h"
#include <QByteArray>
#include <QDebug>
#include <QHostAddress>
udpUser::udpUser(QWidget *parent)
: QMainWindow(parent)
{
layOut();
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::LocalHost, myLine[0]->text().toInt());
connect(sendButton, &QPushButton::clicked,
[=](){
if(textEdit->toPlainText() != "")
{
qint64 size = textEdit->toPlainText().size();
QByteArray msg = textEdit->toPlainText().toUtf8();
udpSocket->writeDatagram(msg.data(), size, QHostAddress(myLine[2]->text()), myLine[1]->text().toInt());
recordText->append("我说:" + textEdit->toPlainText());
textEdit->clear();
}
});
//槽函数处理按键发送和接收处理
//接收数据
connect(udpSocket, &QUdpSocket::readyRead,
[=](){
QByteArray msg;
while(udpSocket->hasPendingDatagrams())
{
msg.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(msg.data(), msg.size());
recordText->append("她说:" + msg);
}
});
}
udpUser::~udpUser()
{
delete udpSocket;
}
void udpUser::layOut()
{
QWidget *window = new QWidget;
window->setObjectName("ud2Widget");
this->setGeometry(900, 100, 480, 800);
labelName << "myPort" << "toPort" << "toIp" ;
labelNum << "9999" << "8888" << "127.0.0.1";
vLayOut = new QVBoxLayout();
gridLayout = new QGridLayout();
for(int i=0; i<3; i++)
{
myLabel[i] = new QLabel();
myLabel[i]->setText(labelName[i]);
myLine[i] = new QLineEdit();
myLine[i]->setText(labelNum[i]);
gridLayout->addWidget(myLabel[i], i, 0);
gridLayout->addWidget(myLine[i], i, 1);
myLabel[i]->setObjectName("ud2Label" + QString::number(i));
}
myLabel[3] = new QLabel("聊天记录");
myLabel[3]->setObjectName("ud2Record");
myLabel[4] = new QLabel("输入内容");
myLabel[4]->setObjectName("ud2Input");
recordText = new QTextEdit();
textEdit = new QTextEdit();
sendButton = new QPushButton();
sendButton->setText("发送");
sendButton->setObjectName("ud2Send");
recordText->setReadOnly(true);
textEdit->setMaximumHeight(200);
vLayOut->addLayout(gridLayout);
vLayOut->addWidget(myLabel[3]);
vLayOut->addWidget(recordText);
vLayOut->addWidget(myLabel[4]);
vLayOut->addWidget(textEdit);
vLayOut->addWidget(sendButton, 0, Qt::AlignRight);
window->setLayout(vLayOut);
this->setCentralWidget(window);
}
ud2代码实现同ud1
main.cpp
#include "mainwindow.h"
#include "udp2.h"
#include <QFile>
#include <QDebug>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFile file(":/style.qss");
if(file.exists())
{
qDebug() << "qss file exits" << endl;
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
qApp->setStyleSheet(styleSheet);
file.close();
}
MainWindow w;
udpUser user2;
user2.show();
w.show();
return a.exec();
总结
代码中涉及的变量转换参考文章:
|