如何避免在Qt中单击按钮两次

如何避免在Qt中单击按钮两次,qt,button,Qt,Button,在我的Qt应用程序中,我有一个启动作业的按钮,需要10秒才能完成。我注意到,在单击按钮后的10秒期间,如果我再次单击按钮区域,它仍然被视为单击,并且作业将在第一次单击结束后立即再次启动 这是预期的吗?在完成第一次单击后的作业之前,如何避免按钮单击?谢谢。您可以使用“设置启用”和“设置禁用”方法在作业执行时停用按钮,然后添加回调以在作业完成时激活按钮 阅读此处的setEnabled文档:您可以使用“setEnabled”和“setDisabled”方法在作业执行时停用按钮,然后添加回调以在作业完成

在我的Qt应用程序中,我有一个启动作业的按钮,需要10秒才能完成。我注意到,在单击按钮后的10秒期间,如果我再次单击按钮区域,它仍然被视为单击,并且作业将在第一次单击结束后立即再次启动

这是预期的吗?在完成第一次单击后的作业之前,如何避免按钮单击?谢谢。

您可以使用“设置启用”和“设置禁用”方法在作业执行时停用按钮,然后添加回调以在作业完成时激活按钮

阅读此处的setEnabled文档:

您可以使用“setEnabled”和“setDisabled”方法在作业执行时停用按钮,然后添加回调以在作业完成时激活按钮


阅读此处setEnabled的文档:

我将禁用该按钮,直到作业完成,并添加一个标签(或GIF动画)以指示“正在进行的工作”。这对用户更有利,因为他看到按钮被禁用-->即他从您的系统获得“反馈”。然后,当流程完成时,您会发出一个“finished”(完成)信号,该信号连接到“enableButtons”(启用按钮)插槽-->即,流程完成后,您会重新启用按钮

void Process(){
 ui->yourButton->setEnabled(false);
/* Maybe play loading gif animation etc*/
/* Do your work here */
emit ProcessFinished();
}

我会禁用该按钮,直到作业完成,并添加一个标签(或GIF动画)以指示“正在进行的工作”。这对用户更有利,因为他看到按钮被禁用-->即他从您的系统获得“反馈”。然后,当流程完成时,您会发出一个“finished”(完成)信号,该信号连接到“enableButtons”(启用按钮)插槽-->即,流程完成后,您会重新启用按钮

void Process(){
 ui->yourButton->setEnabled(false);
/* Maybe play loading gif animation etc*/
/* Do your work here */
emit ProcessFinished();
}

在探究它之后,我认为“QCoreApplication::processEvents()”就做到了。以下是我的发现,也是对主题的总结:

=========================

void MainWindow::on_pushButton_2_clicked()
{
    QDateTime dateTime;
    qDebug()<<"Btn2-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn2-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
}
void MainWindow::on_pushButton_clicked()
{
    QDateTime dateTime;
    ui->pushButton->setEnabled(false);

    qDebug()<<"Btn1-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn1-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QCoreApplication::processEvents();
    ui->pushButton->setEnabled(true);
void MainWindow::on_pushButton_3_clicked()
{
    QDateTime dateTime;
    ui->pushButton_3->setEnabled(false);

    qDebug()<<"Btn3-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");;
    QThread::sleep(10);
    qDebug()<<"Btn3-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    //QCoreApplication::processEvents();
    ui->pushButton_3->setEnabled(true);
}
=========================

void MainWindow::on_pushButton_2_clicked()
{
    QDateTime dateTime;
    qDebug()<<"Btn2-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn2-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
}
void MainWindow::on_pushButton_clicked()
{
    QDateTime dateTime;
    ui->pushButton->setEnabled(false);

    qDebug()<<"Btn1-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn1-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QCoreApplication::processEvents();
    ui->pushButton->setEnabled(true);
void MainWindow::on_pushButton_3_clicked()
{
    QDateTime dateTime;
    ui->pushButton_3->setEnabled(false);

    qDebug()<<"Btn3-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");;
    QThread::sleep(10);
    qDebug()<<"Btn3-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    //QCoreApplication::processEvents();
    ui->pushButton_3->setEnabled(true);
}
=========================

void MainWindow::on_pushButton_2_clicked()
{
    QDateTime dateTime;
    qDebug()<<"Btn2-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn2-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
}
void MainWindow::on_pushButton_clicked()
{
    QDateTime dateTime;
    ui->pushButton->setEnabled(false);

    qDebug()<<"Btn1-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn1-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QCoreApplication::processEvents();
    ui->pushButton->setEnabled(true);
void MainWindow::on_pushButton_3_clicked()
{
    QDateTime dateTime;
    ui->pushButton_3->setEnabled(false);

    qDebug()<<"Btn3-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");;
    QThread::sleep(10);
    qDebug()<<"Btn3-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    //QCoreApplication::processEvents();
    ui->pushButton_3->setEnabled(true);
}
=========================

void MainWindow::on_pushButton_2_clicked()
{
    QDateTime dateTime;
    qDebug()<<"Btn2-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn2-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
}
void MainWindow::on_pushButton_clicked()
{
    QDateTime dateTime;
    ui->pushButton->setEnabled(false);

    qDebug()<<"Btn1-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn1-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QCoreApplication::processEvents();
    ui->pushButton->setEnabled(true);
void MainWindow::on_pushButton_3_clicked()
{
    QDateTime dateTime;
    ui->pushButton_3->setEnabled(false);

    qDebug()<<"Btn3-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");;
    QThread::sleep(10);
    qDebug()<<"Btn3-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    //QCoreApplication::processEvents();
    ui->pushButton_3->setEnabled(true);
}

谢谢大家!

在戳过它之后,我认为“QCoreApplication::processEvents()”就做到了。以下是我的发现,也是对主题的总结:

=========================

void MainWindow::on_pushButton_2_clicked()
{
    QDateTime dateTime;
    qDebug()<<"Btn2-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn2-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
}
void MainWindow::on_pushButton_clicked()
{
    QDateTime dateTime;
    ui->pushButton->setEnabled(false);

    qDebug()<<"Btn1-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn1-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QCoreApplication::processEvents();
    ui->pushButton->setEnabled(true);
void MainWindow::on_pushButton_3_clicked()
{
    QDateTime dateTime;
    ui->pushButton_3->setEnabled(false);

    qDebug()<<"Btn3-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");;
    QThread::sleep(10);
    qDebug()<<"Btn3-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    //QCoreApplication::processEvents();
    ui->pushButton_3->setEnabled(true);
}
=========================

void MainWindow::on_pushButton_2_clicked()
{
    QDateTime dateTime;
    qDebug()<<"Btn2-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn2-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
}
void MainWindow::on_pushButton_clicked()
{
    QDateTime dateTime;
    ui->pushButton->setEnabled(false);

    qDebug()<<"Btn1-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn1-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QCoreApplication::processEvents();
    ui->pushButton->setEnabled(true);
void MainWindow::on_pushButton_3_clicked()
{
    QDateTime dateTime;
    ui->pushButton_3->setEnabled(false);

    qDebug()<<"Btn3-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");;
    QThread::sleep(10);
    qDebug()<<"Btn3-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    //QCoreApplication::processEvents();
    ui->pushButton_3->setEnabled(true);
}
=========================

void MainWindow::on_pushButton_2_clicked()
{
    QDateTime dateTime;
    qDebug()<<"Btn2-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn2-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
}
void MainWindow::on_pushButton_clicked()
{
    QDateTime dateTime;
    ui->pushButton->setEnabled(false);

    qDebug()<<"Btn1-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn1-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QCoreApplication::processEvents();
    ui->pushButton->setEnabled(true);
void MainWindow::on_pushButton_3_clicked()
{
    QDateTime dateTime;
    ui->pushButton_3->setEnabled(false);

    qDebug()<<"Btn3-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");;
    QThread::sleep(10);
    qDebug()<<"Btn3-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    //QCoreApplication::processEvents();
    ui->pushButton_3->setEnabled(true);
}
=========================

void MainWindow::on_pushButton_2_clicked()
{
    QDateTime dateTime;
    qDebug()<<"Btn2-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn2-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
}
void MainWindow::on_pushButton_clicked()
{
    QDateTime dateTime;
    ui->pushButton->setEnabled(false);

    qDebug()<<"Btn1-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QThread::sleep(10);
    qDebug()<<"Btn1-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    QCoreApplication::processEvents();
    ui->pushButton->setEnabled(true);
void MainWindow::on_pushButton_3_clicked()
{
    QDateTime dateTime;
    ui->pushButton_3->setEnabled(false);

    qDebug()<<"Btn3-Time#1:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");;
    QThread::sleep(10);
    qDebug()<<"Btn3-Time#2:"<<dateTime.currentDateTime().toString("[yyyyMMdd hh:mm:ss] ");
    //QCoreApplication::processEvents();
    ui->pushButton_3->setEnabled(true);
}

谢谢大家!

添加回调以激活按钮是什么意思?我尝试将setEnabled(false)放在slot函数的开头,然后将setEnabled(true)放在slot函数的结尾,这没有什么区别。我认为第二次点击被认为是作业结束后的点击,此时按钮被启用,作业再次启动…如果按钮被禁用,“点击”事件应被取消。回调将是对按钮的setEnabled()方法的任何调用。@是的,如果在任务执行期间没有执行事件循环,则只有在任务完成后才会处理所有输入事件。您可以在任务期间调用
QCoreApplication::processEvents()
,尤其是当您希望UI发生更改时(例如在启用/禁用按钮之后)。或者您可以将任务移动到另一个线程,以便该任务不会影响UI事件循环(请参阅QThread、QThreadPool和QtConcurrent)。添加回调以激活按钮是什么意思?我尝试将setEnabled(false)放在slot函数的开头,然后将setEnabled(true)放在slot函数的结尾,这没有什么区别。我认为第二次点击被认为是作业结束后的点击,此时按钮被启用,作业再次启动…如果按钮被禁用,“点击”事件应被取消。回调将是对按钮的setEnabled()方法的任何调用。@是的,如果在任务执行期间没有执行事件循环,则只有在任务完成后才会处理所有输入事件。您可以在任务期间调用
QCoreApplication::processEvents()
,尤其是当您希望UI发生更改时(例如在启用/禁用按钮之后)。或者,您可以将任务移动到另一个线程,以便该任务不会影响UI事件循环(请参阅QThread、QThreadPool和QtConcurrent)。您必须禁用相应插槽中的按钮,并在相关作业完成后再次启用该按钮。这第一步很容易。对于第二阶段,您需要知道作业何时完成。您必须禁用相应插槽中的按钮,并在相关作业完成后再次启用该按钮。这第一步很容易。对于第二阶段,你需要知道工作何时完成。我认为这应该是可行的。然而,在添加“QCoreApplication::processEvents()”的注释之一中的另一种方式似乎更简单……我认为这应该是可行的。然而,在添加“QCoreApplication::processEvents()”的注释之一中的另一种方式似乎更简单。。。