Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - change cursor to hourglass and disable cursor

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?

like image 720
fucai1116 Avatar asked Jul 05 '11 09:07

fucai1116


1 Answers

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);
like image 113
Thomas Vincent Avatar answered Sep 21 '22 15:09

Thomas Vincent