YeP
YeP

Reputation: 211

"please wait" during long operation in Qt?

I need to do an operation after a user clicking a button, and that may take some time. How can I display a "busy" or "please wait" icon/message (like a spinning circle etc) and prevent a user from clicking the button again (or some other buttons) during the time? Thanks.

Upvotes: 1

Views: 1762

Answers (1)

Pavan Chandaka
Pavan Chandaka

Reputation: 12761

Use your QApplication object's static function setOverrideCursor(const QCursor &cursor)

Set the wait cursor, to make the application "busy", as shown below.

QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));

And restore using below function, when you feel the work is done

QApplication::restoreOverrideCursor();

some documentation help:

http://doc.qt.io/qt-5/qguiapplication.html#setOverrideCursor

Now to disable the window, override the event filter function of your window class.

declare a global variable, that says "busy" or "notbusy". ex: _isBusy.

In the event filter do something as shown below.

eventfilter is a virtual function of QObject.

So you can override in your window class as shown below.

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
     if (_isBusy) //your global variable to check "busy or "notbusy"
     {
         //Just bypass key and mouse events
          switch(event->type())
          {
             case QEvent::MouseButtonPress:
             case QEvent::MouseButtonRelease:
             case QEvent::KeyPress:
             case QEvent::KeyRelease:
             return true;
          }
     }
}

Note: "MainWindow" in the above function declaration is your class name. Also in the switch statement, I limited the count to only 4 events, add as many events you want.

Upvotes: 5

Related Questions