Home > Back-end >  How to realize a prompt dialog box when load a large file or other things using Qt?
How to realize a prompt dialog box when load a large file or other things using Qt?

Time:04-23

I need to load a large file to parse and draw with OpenGL. The whole process is very time-consuming. So I want to realize a prompt dialog box before parse. But the code as following is not work.

void parseFile()
{
    QMessageBox* msgbox = new QMessageBox();
    msgbox->setModal(false);
    msgbox->setWindowTitle(tr("message box"));
    msgbox->setText("Please wait patiently......")
    msgbox->show();

    /* parse file and draw */
    ......
}

But it shows like also be frozen: enter image description here

How to realize it?

CodePudding user response:

show() does not actually show the content of the dialog. It only tells the event loop to show the dialog ASAP. But since you call more code after immediately after show(), the evnt loop does not have a chance to do its work.

The easiest way to solve this is to call QCoreApplication::processEvents() after msgbox->show(). This will force the event loop to do the work immediately.

Another option would be to move the heavy calculation to a function and than show the dialog and then schedule the calculation using a timer.

...
msgbox->show();
QTimer::singleShot(0, &doHeavyWork());

This would first handle all events related to showing the dialog and only after that it will start the heavy work (i.e. parsing the file).

  •  Tags:  
  • qt
  • Related