- 博客(1)
- 资源 (1)
- 收藏
- 关注
原创 基于QT的UDP网络聊天室(QQ界面)
QT网络聊天室(udp)1.第一部分界面部分第一部分的QT(QQ)界面部分的控件分别有登录是一个button类用于发送登录信息lineEdit类用于接收用户名,还有一些装饰的小控件具体请看UI文件首先1.我们实现了一个根据鼠标滑动可以拖动主窗口的功能二。第二部分窗口滑动事件使用鼠标拖动事件与鼠标按压事件的一个交互请注意这里的按压点应该减去我们的鼠标中心点达到一个拖动窗口而不更换窗口位...
2019-08-27 09:42:31 3383
QQ (6).zip
本片文章的代码#include "chatwindow.h"
#include "ui_chatwindow.h"
#include
#include
#include
#include
#include
#include
#include
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:" <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(QHostAddres
2019-08-26
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人