CodeLibrary/04_QtCreator_Cplus_TCPClient_20240423/cheat.cpp
2024-04-24 23:08:17 +08:00

42 lines
1.0 KiB
C++

#include "cheat.h"
#include "ui_cheat.h"
cheat::cheat(QTcpSocket *s, QWidget *parent) :
QWidget(parent),
ui(new Ui::cheat)
{
ui->setupUi(this);
// 将传入的参数存储至 socket
socket = s;
}
cheat::~cheat()
{
delete ui;
}
// 清除信息区内容
void cheat::on_Clear_Button_clicked()
{
ui->Message_Edit->clear();
}
void cheat::on_Send_Button_clicked()
{
// 实例化 QByteArray 对象
QByteArray ba;
// 获取当前系统时间
QDateTime curremt_date = QDateTime::currentDateTime();
// 获取目标服务器 IP
QString target_ip = socket->peerAddress().toString();
// 获取用户输入并且转换为 QByteArray 类型
ba.append(ui->Input_Edit->text());
// 写入数据
socket->write(ba);
// 提示用户发送的消息
ui->Message_Edit->insertPlainText("\nSend Data to " + target_ip + " in " + curremt_date.toString("yyyy-MM-dd hh:mm:ss")
+ "\n-> " + ui->Input_Edit->text());
// 写入完毕清空输入框
ui->Input_Edit->clear();
}