简体   繁体   中英

Qt text browser not displays in real time

I wrote a function to calculate some values and used append to write them to the textBrowser every time a value was calculated.

What I want is that every time the append executes, data is displayed in the textBrowser simultaneously.

However, all data display at once when the function ends. Not one by one.

Also, I have a push button. I want it to change its text when it is pushed, and change again when its function is done. So I wrote:

void MainWindow::on_btn_clicked(){
    ui->btn->setText("Running...");
    //some codes
    ui->btn->setText("Reset");
}

But it only changes button's text to "Reset" when the clicked function is over.

I think these two problem might be relevant, but I can't find a solution.

The GUI does not get updated until the control returns to the event loop. So when appending to the text browser they are all updated once the main event loop is executed. This is also true for setting text of the button. When on_btn_clicked() is called, the text of the button does not get updated until the function is finished.

If you have some codes that need long time to be processed, then you should not put it in the main thread. It should be done in an other thread which interacts with the main thread through signals and slots in order to report some progress or update GUI elements.

You need to understand what is going in your program.

First of all, take a look at Threading Basics . After, you can choose approach you want from Multithreading Technologies in Qt .

In few words, your problem is that you make some blocking long-term calculation in your main thread. To avoid it you must provide all calculations in separate thread. Here is sufficient answer to your question: https://stackoverflow.com/a/1386160/867349 .

Easiest but not good way is to place QCoreApplication::processEvents() for example inside your //some codes 's cycle. Hopefully, you'll find better way like QtCuncurrentRun or preparing working thread in links above .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM