#include "mainwindow.h"
#include "ui_mainwindow.h"
extern QString myNickname;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
tcpSocket =NULL;
//分配空间指定父对象
tcpSocket = new QTcpSocket(this);
isStart = true; //是没有传文件状态
sendForName = ""; //聊天对象
haveOtherName = false; //有聊天对象
ui->label_3->setText(QString("我是%1").arg(myNickname));
connect(tcpSocket,&QTcpSocket::connected,[=]()
{
//qDebug()<<"yyyy";
ui->msgRead->append("成功和服务器建立好连接");
link(); //发送建立连接函数
});
//读数据连接
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readData()));
//定时器连接槽函数发送文件
pathTimer = new QTimer(this);
connect(pathTimer,SIGNAL(timeout()),this,SLOT(TimeSendData()));
}
MainWindow::~MainWindow()
{
delete ui;
}
//连接
void MainWindow::on_pushButton_connect_clicked()
{
QString ip = ui->lineEdit_ip->text();
qint16 port = ui->lineEdit_port->text().toInt();
tcpSocket->connectToHost(QHostAddress(ip),port);
}
//发送文本点击
void MainWindow::on_pushButton_send_clicked()
{
if(haveOtherName == true)
{
QString str = ui->msgWrite->toPlainText();
//组包发送
QString sendStr = "AAAA"+sendForName+"##"+str;
tcpSocket->write(sendStr.toUtf8().data());
ui->msgWrite->clear();
ui->msgWrite->setFocus();
}else {
QMessageBox::information(NULL,"提示信息","先选择需要发送的用户");
}
}
//读数据槽
void MainWindow::readData()
{
QByteArray buf = tcpSocket->readAll();
if(true == isStart)
{
QString strbuf = QString(buf);
qDebug()<<strbuf;
if(strbuf.mid(0,4) == "AAAA") //普通聊天
{
QString mycmd = strbuf.mid(4);
QString otherName = mycmd.section("##",0,0);
QString text = mycmd.section("##",1,1);
QDateTime time = QDateTime::currentDateTime();
QString strtime = time.toString("yyyy-MM-dd hh:mm:ss");
ui->msgRead->setTextColor(QColor(Qt::green)); //变成绿色字体
ui->msgRead->append(QString("[%1%2发布的私聊]::%3").arg(strtime).arg(otherName).arg(text));
}
else if(strbuf.mid(0,4) == "BBBB") //上线
{
QString otherName = strbuf.mid(4);
QString str = otherName+"已上线";
QDateTime time = QDateTime::currentDateTime();
QString strtime = time.toString("yyyy-MM-dd hh:mm:ss");
ui->msgRead->setTextColor(QColor(Qt::gray)); //变成灰色字体
ui->msgRead->append(QString("[%1]%2").arg(strtime).arg(str));
// 添加
itemForm *dataItem = new itemForm(this);
dataItem->showData(otherName);
connect(dataItem,SIGNAL(nameBtnCleck(QString)),this,SLOT(sendForNameSlot(QString)));
QString itemName = dataItem->giveName();
QListWidgetItem *item = new QListWidgetItem(itemName);
QIcon icon(":/res/check1.ico");
item->setIcon(icon);
item->setSizeHint(QSize(139,37));
ui->listWidget->addItem(item);
ui->listWidget->setItemWidget(item,dataItem);
}else if(strbuf.mid(0,4) == "CCCC") //下线
{
QString otherName = strbuf.mid(4);
QString str = otherName+"已下线";
QDateTime time = QDateTime::currentDateTime();
QString strtime = time.toString("yyyy-MM-dd hh:mm:ss");
ui->msgRead->setTextColor(QColor(Qt::gray)); //变成灰色字体
ui->msgRead->append(QString("[%1]%2").arg(strtime).arg(str));
int count = ui->listWidget->count();
for(int i = 0;i<count;i++)
{
QListWidgetItem *item = ui->listWidget->item(i);
if(item->text() == otherName) //是这个
{
ui->listWidget->removeItemWidget(item);
delete item;
if(otherName == sendForName)
{
haveOtherName = false;
}
return; //退出
}
}
}else if(strbuf.mid(0,4) == "DDDD") //传输文件名
{
QString mycmd = strbuf.mid(4);
isStart = false;
//取到名字
recvfileName = mycmd.section("##",0,0);
recvfileSize = mycmd.section("##",1,1).toInt();
//谁发来的
recvOtherPathName = mycmd.section("##",2,2);
recvSize = 0;
recvfile.setFileName(recvfileName);
bool isok = recvfile.open(QIODevice::WriteOnly);
if(false == isok)
{
qDebug()<<"接收出错";
}
}else if (strbuf.mid(0,4) == "EEEE")
{ //刚上线时,收到在线的名字
QString textname = strbuf.mid(4);
//王五##李四##
textname.chop(2); //删除后面两个字符(##)
QStringList strList = textname.split("##"); //以##分开
foreach (QString s, strList) {
//添加
itemForm *dataItem = new itemForm(this);
dataItem->showData(s);
connect(dataItem,SIGNAL(nameBtnCleck(QString)),this,SLOT(sendForNameSlot(QString)));
QString itemName = dataItem->giveName();
QListWidgetItem *item = new QListWidgetItem(itemName);
QIcon icon(":/res/check1.ico");
item->setIcon(icon);
item->setSizeHint(QSize(139,37));
ui->listWidget->addItem(item);
ui->listWidget->setItemWidget(item,dataItem);
}
}else if (strbuf.mid(0,4) == "FFFF")
{
QString mycmd = strbuf.mid(4);
QString otherName = mycmd.section("##",0,0);
QString text = mycmd.section("##",1,1);
QDateTime time = QDateTime::currentDateTime();
QString strtime = time.toString("yyyy-MM-dd hh:mm:ss");
ui->msgRead->setTextColor(QColor(Qt::blue)); //变成蓝色字体
ui->msgRead->append(QString("[%1%2发布的群消息]::%3").arg(strtime).arg(otherName).arg(text));
}
}else { //接收文件
qint64 len = recvfile.write(buf);
recvSize += len;
if(recvSize == recvfileSize)
{
qDebug()<<"接收完成";
recvfile.close();
isStart = true;
//显示一下
QString str = QString("接收%1传来的文件%2成功").arg(recvOtherPathName).arg(recvfileName);
QDateTime time = QDateTime::currentDateTime();
QString strtime = time.toString("yyyy-MM-dd hh:mm:ss");
ui->msgRead->setTextColor(QColor(Qt::red)); //变成灰色字体
ui->msgRead->append(QString("[%1]%2").arg(strtime).arg(str));
}
}
}
//上线函数
void MainWindow::link()
{
QString str = "BBBB"+myNickname;
tcpSocket->write(str.toUtf8().data());
}
//下线函数
void MainWindow::unlink()
{
QString str = "CCCC"+myNickname;
tcpSocket->write(str.toUtf8().data());
}
//发送文件点击
void MainWindow::on_pushButton_sendpath_clicked()
{
if(haveOtherName == true)
{
QString filePath = QFileDialog::getOpenFileName(this,"open","../");
if(false == filePath.isEmpty())
{
sendfileName.clear();
sendfile
佬酒
- 粉丝: 5703
- 资源: 8
最新资源
- Java字符串转换处理工具类
- windows USB 驱动,用于PL2303芯片上报GPS信息使用
- McFly 为 Bash 提供历史命令搜索功能 v0.9.2
- Package Control-12.22.sublime-package.zip
- Dragon book编译器龙书源码附详细注释
- 华为云开发者服务协议.pdf
- Hyper-YOLO保姆级教程(私以为的YOLOv12)
- Hyper-YOLO保姆级教程(私以为的YOLOv12)
- Java课程课后作业答案(1).zip
- IMG_20230412_094114.jpg
- asm-西电微机原理实验
- py-apple-quadruped-robot-四足机器人
- asm-西电微机原理实验
- asm-西电微机原理实验
- py-apple-bldc-quadruped-robot-四足机器人
- asm-西电微机原理实验
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0