Qt - 将光标更改为沙漏并禁用光标

发布于 2024-11-18 18:03:37 字数 308 浏览 2 评论 0原文

目前我正在开发一个Qt 程序。为了防止用户在长时间任务运行时与应用程序交互,我尝试了 通过调用覆盖光标

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

但是,鼠标单击事件不会被禁用。

有没有办法在不禁用 GUI 的所有小部件的情况下禁用鼠标单击事件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女净妖师 2024-11-25 18:03:37

我花了很多时间来寻找一种方法来实际上阻止 Qt 中的用户交互,事件过滤似乎是一个可以接受的解决方案。

这里有一个例子:

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 );
}

那么当你要锁定:

qapp->setOverrideCursor(Qt::WaitCursor);
qapp->installEventFilter(filter);

和解锁时:

while( qapp->overrideCursor()) //be careful application may have been lock several times ...
    qapp->restoreOverrideCursor();
qapp->removeEventFilter(filter);

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);
给不了的爱 2024-11-25 18:03:37

这是对我的应用程序有帮助的不同方法:在处理用户事件时,通常仅在当前运行的任务调用 Q(Core)Application::processEvents() 时才处理新的鼠标单击/按钮按下。这通常用于强制重新绘制,例如当进度条发生变化时。请注意,这也可能是任何包含的基于 Qt 的第三方库的一部分。

如果将这些调用更改为

QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );

在长时间任务期间发生的鼠标单击,将在任务完成后进行处理。当然,有时您可能不想延迟用户输入事件,因此您必须针对每个 processEvents() 调用进行检查。

如果您的第 3 方库有 processEvents() 调用并且您无法更改它们,那么您将遇到我担心的同样问题。

Here is a different approach that helped for my application: While a user event is being processed, new mouse clicks/button presses are usually only processed when the currently running task calls Q(Core)Application::processEvents(). This is often used in order to force a repaint, e.g. when a progress bar changes. Note that this may be part of any included Qt-based 3rd party libraries, too.

If you change these calls to

QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );

mouse clicks which happen during your long task will be processed after the task is done. Of course there may be occasions where you don't want to delay your user input events so you have to check that for every single processEvents() call.

If your 3rd party libs have processEvents() calls and you cannot change them, you will be stuck with the same problem I fear.

娇妻 2024-11-25 18:03:37

setOverrideCursur 仅更改光标外观。
要禁用用户输入,您可以弹出进度对话框或在事件循环中过滤鼠标/键盘事件。

setOverrideCursur only changes the cursor appearance.
To disable user input, you could pop up a progress dialog or filter mouse / keyboard events in the event loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文