QT做while循环 [英] QT do while loop

查看:1605
本文介绍了QT做while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这几天我正在学习QT,我想测试while循环,正常登录正常,但是在QT中,应用程序冻结了..
我定义了randnum并猜测了头文件(public)

I am learning QT these days and I wanted to test do while loop , the actual login works in normally, but in QT the application freezes .. I have defined randnum and guess in the header file ( public )

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);
}

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

please tell me how to find the reason why it freezes.. thanks !

推荐答案

您的应用程序冻结,是因为您正在循环并且从不让Qt进行必要的处理。

Your application freezes because you are looping and never letting Qt do its necessary processing.

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

You need to set up what the random guess is in the class constructor and then on on the on_pushButton_clicked call you need to just do the if checks. ie remove the do while loop.

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

The code will exit the callback function and then control will return to Qt allowing the user to take another guess.

这篇关于QT做while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆