QT做while循环

Vam*_*a B 0 c++ qt qt4 do-while

我这几天正在学习QT,我想测试do while循环,实际登录正常工作,但在QT中应用程序冻结..我已经定义了randnum并在头文件中猜测(公共)

void MainWindow::on_pushButton_clicked()
{
    srand (time(NULL));
    randnum = rand() % 10 +1;
    do {
    guess = ui->spinBox->value();
    if (guess < randnum)
    {
    ui->label->setText("try something big");
    }
    else if (guess > randnum)
    {
    ui->label->setText("try something small");
    }
    else
        ui->label->setText("YAY?");

    } while (guess != randnum);
}
Run Code Online (Sandbox Code Playgroud)

请告诉我如何找到冻结的原因..谢谢!

Goz*_*Goz 7

您的应用程序冻结,因为您正在循环,并且永远不会让Qt进行必要的处理.

您需要在类构造函数中设置随机猜测的内容,然后在on_pushButton_clicked调用上,您需要执行if检查.即删除do while循环.

代码将退出回调函数,然后控件将返回Qt,允许用户进行另一次猜测.