Currently I am working on a Qt program. To prevent the user from interacting with the application when a long task is running, I tried overriding the cursor by calling
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
However, mouse click events aren't disabled.
Is there any way to disable mouse click events without disabling all widgets of GUI?
I spent a lot of time to find a way to actually prevent user interaction in Qt and it occurs that event filtering seems to be an acceptable solution.
Here an example:
class AppFilter : public QObject
{
protected:
bool eventFilter( QObject *obj, QEvent *event );
};
bool AppFilter::eventFilter(QObject *obj, QEvent *event)
{
switch ( event->type())
{
//list event you want to prevent here ...
case QEvent::KeyPress:
case QEvent::KeyRelease:
case QEvent::MouseButtonRelease:
case QEvent::MouseButtonPress:
case QEvent::MouseButtonDblClick:
//...
return true;
}
return QObject::eventFilter( obj, event );
}
Then when you what to lock:
qapp->setOverrideCursor(Qt::WaitCursor);
qapp->installEventFilter(filter);
And unlock:
while( qapp->overrideCursor()) //be careful application may have been lock several times ...
qapp->restoreOverrideCursor();
qapp->removeEventFilter(filter);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With