完成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();
}

现象:

相关推荐
端平入洛4 小时前
auto有时不auto
c++
哇哈哈20211 天前
信号量和信号
linux·c++
多恩Stone1 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
gihigo19981 天前
基于TCP协议实现视频采集与通信
网络协议·tcp/ip·音视频
蜡笔小马1 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝1 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc1 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼1 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛
yyjtx1 天前
DHU上机打卡D31
开发语言·c++·算法
czxyvX1 天前
020-C++之unordered容器
数据结构·c++