Qt同步处理业务并禁用按钮

1.界面+代码

cpp 复制代码
//按钮1
void Dialog::on_pushButton1_clicked()
{
    qDebug("pushButton1 clicked start");
    enableBtns(false);//禁用按钮
    qDebug("pushButton1 do sth start");
    QThread::sleep(5);//休眠,作为同步处理业务
    qDebug("pushButton1 do sth end");
    enableBtns(true);//启用按钮
    qDebug("pushButton1 clicked end");
}
//按钮2
void Dialog::on_pushButton2_clicked()
{
    qDebug("pushButton2 clicked start");
    enableBtns(false);
    //singleShot 延后处理业务。时间0ms不行
    QTimer::singleShot(1,this,[this](){
        qDebug("pushButton2 do sth start");
        QThread::sleep(5);
        qDebug("pushButton2 do sth end");
        enableBtns(true);
    });
    qDebug("pushButton2 clicked end");
}
//按钮3
void Dialog::on_pushButton3_clicked()
{
    qDebug("pushButton3 clicked start");
    enableBtns(false);
    //singleShot 延后处理业务。时间0ms不行
    QTimer::singleShot(1,this,[this](){
        qDebug("pushButton3 do sth start");
        QThread::sleep(5);
        qDebug("pushButton3 do sth end");
        //singleShot 延后启用按钮。时间 0或1不行,建议为5或10以上
        QTimer::singleShot(5,this,[this](){
            enableBtns(true);
        });
    });
    qDebug("pushButton3 clicked end");
}
//按钮其他
void Dialog::on_pushButtonOther_clicked()
{
    qDebug("pushButtonOther clicked");
}
//按钮禁用状态设置
void Dialog::enableBtns(bool enable)
{
    qDebug(enable?"enableBtns":"disableBtns");
    ui->pushButton1->setEnabled(enable);
    ui->pushButton2->setEnabled(enable);
    ui->pushButton3->setEnabled(enable);
    ui->pushButtonOther->setEnabled(enable);
}

2.测试

2.1点击 "按钮1",然后立即点击"按钮其他"。按钮没变成禁用状态,"按钮其他"触发。

bash 复制代码
pushButton1 clicked start
disableBtns
pushButton1 do sth start
pushButton1 do sth end
enableBtns
pushButton1 clicked end
pushButtonOther clicked
pushButtonOther clicked

2.2点击 "按钮2",按钮变成禁用状态,然后点击"按钮其他","按钮其他"仍触发。

bash 复制代码
pushButton2 clicked start
disableBtns
pushButton2 clicked end
pushButton2 do sth start
pushButton2 do sth end
enableBtns
pushButtonOther clicked
pushButtonOther clicked

2.2点击 "按钮3",按钮变成禁用状态,然后点击"按钮其他","按钮其他"未触发。

bash 复制代码
pushButton3 clicked start
disableBtns
pushButton3 clicked end
pushButton3 do sth start
pushButton3 do sth end
enableBtns

3.原因分析

Qt的界面刷新、按钮或QTimer::singleShot的业务处理都是放在主线程处理的。

主线程处理按钮业务时,不会处理其他业务,界面也不会刷新。此时如果有其他事件,会将其他事件放在义务处理完。

业务处理前禁用按钮,业务处理中就不会刷新界面样式;如果业务中点击过按钮,当业务处理完后有立即启用按钮,Qt框架会处理点击事件,判断按钮已经可用了,就会再次调用点击业务。

QTimer::singleShot 可以将业务延后处理,使界面样式刷新,业务处理,点击事件判断 按预定顺序处理。

相关推荐
一叶之秋141219 小时前
QT背景介绍与环境搭建
开发语言·qt
QT 小鲜肉20 小时前
【QT/C++】Qt网络编程进阶:UDP通信和HTTP请求的基本原理和实际应用(超详细)
c语言·网络·c++·笔记·qt·http·udp
四维碎片1 天前
【Qt】大数据量表格刷新优化--只刷新可见区域
开发语言·qt
一叶之秋14121 天前
Qt开发初识
开发语言·qt
梵尔纳多1 天前
ffmpeg 使用滤镜实现播放倍速
c++·qt·ffmpeg
QT 小鲜肉1 天前
【QT/C++】Qt网络编程进阶:TCP网络编程的基本原理和实际应用(超详细)
c语言·开发语言·网络·c++·qt·学习·tcp/ip
Tony小周2 天前
使用QKeyEvent keyPress(QEvent::KeyPress, key模拟键盘发送事件,会导致主程序卡死
嵌入式硬件·qt
Larry_Yanan2 天前
QML学习笔记(五十)QML与C++交互:QML中单例C++对象
开发语言·c++·笔记·qt·学习·ui·交互
zhmhbest2 天前
Qt 全球峰会 2025:中国站速递 —— 技术中立,拥抱更大生态
开发语言·qt·系统架构
feiyangqingyun2 天前
Qt实时绘制飞行轨迹/移动轨迹实时显示/带旋转角度/平滑移动/效果一级棒/地面站软件开发/无人机管理平台
qt·无人机·集群地面站