Qt - 将光标更改为沙漏并禁用光标
目前我正在开发一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我花了很多时间来寻找一种方法来实际上阻止 Qt 中的用户交互,事件过滤似乎是一个可以接受的解决方案。
这里有一个例子:
那么当你要锁定:
和解锁时:
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:
Then when you what to lock:
And unlock:
这是对我的应用程序有帮助的不同方法:在处理用户事件时,通常仅在当前运行的任务调用 Q(Core)Application::processEvents() 时才处理新的鼠标单击/按钮按下。这通常用于强制重新绘制,例如当进度条发生变化时。请注意,这也可能是任何包含的基于 Qt 的第三方库的一部分。
如果将这些调用更改为
在长时间任务期间发生的鼠标单击,将在任务完成后进行处理。当然,有时您可能不想延迟用户输入事件,因此您必须针对每个 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
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.
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.