QQ (6).zip
本片文章的代码#include "chatwindow.h" #include "ui_chatwindow.h" #include <QDebug> #include <QJsonDocument> #include <QJsonParseError> #include <QJsonObject> #include <QHostInfo> #include<QWidgetItem> #include<QTime> ChatWindow::ChatWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::ChatWindow) { ui->setupUi(this); destIP = "255.255.255.0"; destport=8888; udpSocket = new QUdpSocket(this); bool ok = udpSocket->bind(8888); if(!ok){ qDebug() << "Fail to bind port:8888 , error:" << udpSocket->errorString(); return; } connect(udpSocket,SIGNAL(readyRead()),this,SLOT(recvMessage())); } ChatWindow::~ChatWindow() { delete ui; } void ChatWindow::processTheDatagram(const QByteArray &data;,const QHostAddress &sender;, const int senderPort) { //将我们的接收到的json包借读根据不同的message_type值来选择相应的类容 QJsonParseError parseError; qDebug()<<senderPort; QJsonDocument jsondoc = QJsonDocument::fromJson(data,&parseError;); if(jsondoc.isNull() || parseError.error != QJsonParseError::NoError){ qDebug() << "Fail to parse json data : " << parseError.errorString(); return; } QJsonObject jsonObject = jsondoc.object(); int message_type = jsonObject["message_type"].toInt(); QString name = jsonObject["name"].toString(); QString content = jsonObject["content"].toString(); short port = (short)jsonObject["port"].toInt(); switch(message_type){ case ChatWindow::LOGIN_MESSAGE: //添加登陆的用户到好友列表 //给登陆的用户发送一个应答消息,告诉他自己在线 doUserLogin(QHostAddress(sender.toIPv4Address()),name,port); break; case ChatWindow::CHAT_MESSAGE: //显示聊天的消息 break; case ChatWindow::ACK_MESSAGE: //添加应答用户到好友列表 doUserAck(QHostAddress(sender.toIPv4Address()),name,port); return; case ChatWindow::QUIT_MESSAGE: //将退出的用户从好友列表中删除 doUserQuit(QHostAddress(sender.toIPv4Address()),name,port); break; } displaySendChatMessage(username,content); return; } //实现第一个功能将显示好友和自己的上线信息。有 void ChatWindow::doUserLogin(const QHostAddress &ip;, const QString &name;,short port) { //添加登陆的用户到好友列表 addUserToList(ip,name,port); if(!checkSelfIp(ip)){ //告诉登陆的用户,自己在线 tellLoginUser(ip,port); } return; } //添加用户到好友链表中去 void ChatWindow::doUserAck(const QHostAddress &ip;, const QString &name;, short port) { addUserToList(ip,name,port); } void ChatWindow::doUserQuit(const QHostAddress &ip;, const QString &name;, short port) { QString text = QString("%1\n%2:%3").arg(name).arg(ip.toString()).arg(port); QList<QListWidgetItem *> itemList = ui->listWidget->findItems(text,Qt::MatchExactly); for(int i = 0;i < itemList.size();i ++){ ui->listWidget->removeItemWidget(itemList[i]); delete itemList[i]; } return; } //加入一个listwight void ChatWindow::addUserToList(const QHostAddress &ip;, const QString &name;, short port) { QString text = QString("%1\n%2:%3").arg(name).arg(ip.toString()).arg(port); //一种输出文本格式可以相当于printf ui->listWidget->addItem(new QListWidgetItem(QIcon(":/image/HeadImage.png"),text)); //加图片在listWidget中去 ui->listWidget->setIconSize(QSize(50,50));
- 1
- 粉丝: 4
- 资源: 1
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理