0

In my loadDatabase function, I have clearly set the status to display the message "loading coverage database..." before setOverrideCursor(Qt::WaitCursor), but the status bar is only displaying the message after restoreOverrideCursor() is called. I have tried various ways to display the message before setOverrideCursor(Qt::WaitCursor), but I'm unsure of the reason behind this behavior.

void loadDatabase(const QString &name) {
    //setting statusBar's message
    statusBar()->showMessage("loading coverage database...");

    QApplication::setOverrideCursor(Qt::WaitCursor);
    //loading the database and setting up the GUI are CPU-intensive operations.
    m_logger_widget->clear();
    m_logger_widget->info("opening database %s", name.toStdString().data());
    auto coverage_database = new CoverageDatabase(name, m_logger_widget);
    m_instance_view->setModel(coverage_database->getInstanceModel());
    QApplication::restoreOverrideCursor();
}

I hope to identify the reason why "loading coverage database..." is not displayed and to have it shown before setting the WaitCursor.

piggogo
  • 11
  • 2
  • you want to run a task and show a loading indicator. But you block the main thread. for this you have to swap the task into a thread/async task... – Marco F. Jul 14 '23 at 10:43
  • But I'm displaying the "loading" message before setting the waitCursor. According to sequential order, shouldn't the task be completed first before blocking the main thread based on waitCursor?@MarcoF. – piggogo Jul 16 '23 at 08:40

1 Answers1

0

Your loadDatabase() function runs in the main GUI thread, it blocks all further GUI updates until it completes.

One possibility is to call the function QCoreApplication::processEvents() after you have set the status message.

https://doc.qt.io/qt-6/qcoreapplication.html#processEvents

However, I would outsource the loading to an asynchronous task/thread. This way you also get the possibility to abort the loading process.

Marco F.
  • 432
  • 1
  • 10
  • 1
    Thank you very much for your suggestion. It solved my problem. I followed your advice and added QCoreApplication::processEvents() after setting the message. Previously, I also tried using it within the waitCursor block, but it didn't work. However, after carefully reading the documentation you provided, it states: "Available events are events queued before the function call." I didn't choose to use an asynchronous approach because there is only one place in the project where it needs to be handled, and my supervisor advised against involving threads. – piggogo Jul 17 '23 at 05:33