完成qt 的TCP客户端的断开连接

实现部分代码:

cpp 复制代码
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    ,cli(new QTcpSocket(this))
{
    ui->setupUi(this);
    ui->sendbut->setEnabled(false);
    ui->msgedit->setEnabled(false);
    ui->disconbut->setEnabled(false);
    Widget::connect(cli,&QTcpSocket::connected,this,&Widget::connect_slot);
    Widget::connect(cli,&QTcpSocket::readyRead,this,&Widget::ReadyRead_slot);
//建立断开连接信号和槽函数的信号与槽连接
    Widget::connect(cli,&QTcpSocket::disconnected,this,&Widget::disconnect_slot);
}
//取消按钮按下槽函数
void Widget::on_disconbut_clicked()
{
    //告诉服务器断开连接
    QString msg = usrname + ": 离开聊天室";
    //将数据发送给服务器
    cli->write(msg.toLocal8Bit());
//与服务器断开连接
    cli->disconnectFromHost();
}
//断开连接信号槽函数
void Widget::disconnect_slot()
{
//更新状态栏
    ui->statlab->setText("未连接服务器");
//改变组件使能
    ui->usrnameedit->setEnabled(true);
    ui->sipedit->setEnabled(true);
    ui->sportedit->setEnabled(true);
    ui->sendbut->setEnabled(false);
    ui->msgedit->setEnabled(false);
    ui->disconbut->setEnabled(false);
//清空文本
    ui->msgedit->clear();
    ui->usrnameedit->clear();
    ui->sipedit->clear();
    ui->sportedit->clear();
}

现象:

相关推荐
Yunovian4 小时前
AI时代,针对模型与应用,浅谈一下各编程语言
开发语言·c++·人工智能·python·ai·rust·ai编程
今年下半年7 小时前
记录一次IBMS物联网项目的架构设计及技术栈应用
物联网·websocket·网络协议·tcp/ip·微服务·udp·kafka
Quz7 小时前
QML TreeView:排序与搜索过滤
qt
Quz8 小时前
QML TreeView:基础树视图与自定义委托
qt
UIU1149 小时前
递归算法与汉诺塔
c++·学习·算法·c#·递归
Fruit_Caller9 小时前
static
开发语言·c++
Junfeng的技术笔记14 小时前
基于 C++/Qt 的雷达回波信号检测与测距仿真系统
qt
Quz14 小时前
QML TableView:自定义样式与排序表格
qt
Quz14 小时前
QML TableView:可编辑表格与 SQLite 数据持久化
qt·sqlite
Quz14 小时前
QML ListView:C++ 数据模型与 ProxyModel 实时过滤
qt