Search code examples
c++qtupdatingqtextbrowser

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.


Solution

  • 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.