在 Qt 中显示窗口而不窃取焦点

发布于 2024-07-23 08:43:47 字数 148 浏览 2 评论 0原文

当用户不使用第二台显示器时,我使用 Qt 库在第二台显示器上显示幻灯片。 一个示例是用户在第一个显示器中玩游戏并在第二个显示器中显示幻灯片。

问题是,当我在 Qt 中打开一个新窗口时,它会自动窃取前一个应用程序的焦点。 有什么办法可以防止这种情况发生吗?

I'm using the Qt library to show a slideshow on the second monitor when the user isn't using the second monitor. An example is the user playing a game in the first monitor and showing the slideshow in the second monitor.

The problem is that when I open a new window in Qt, it automatically steals the focus from the previous application. Is there any way to prevent this from happening?

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

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

发布评论

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

评论(3

热血少△年 2024-07-30 08:43:47

我花了一段时间才找到它,但我找到了它: setAttribute(Qt::WA_ShowWithoutActivating);

这会强制窗口不激活。 即使使用 Qt::WindowStaysOnTopHint 标志

It took me a while to find it but I found it: setAttribute(Qt::WA_ShowWithoutActivating);

This forces the window not to activate. Even with the Qt::WindowStaysOnTopHint flag

舟遥客 2024-07-30 08:43:47

如果您想制作浮动预览框/任何其他小部件,只需使用下面的

thumbnail = new QLabel;
thumbnail->setAttribute(Qt::WA_ShowWithoutActivating);
thumbnail->setParent(0);
thumbnail->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);

Qt::Tool 是使其工作的重要标志。 我的意思是不抢焦点。

If you want to make floating preview box/ any other widget just use below

thumbnail = new QLabel;
thumbnail->setAttribute(Qt::WA_ShowWithoutActivating);
thumbnail->setParent(0);
thumbnail->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);

Qt::Tool is important flag to make it work. I mean not stealing focus.

你好,陌生人 2024-07-30 08:43:47

默认情况下,小部件不接受焦点,但您可能还没有创建普通的小部件? 它是哪个子类? QMainWindow 还是其他什么?

窗口子类可能默认接受焦点,因此在调用 QWidget::show() 之前尝试使用 Qt::NoFocus 显式调用 QWidget::setFocusPolicy。

另外,请确保您在任何时候都没有在窗口或其任何小部件上调用 QWidget::activateWindow() 。

Widgets don't accept focus by default but presumably you haven't created a plain widget? Which subclass was it? QMainWindow or something else?

It's possible the window subclasses default to accepting focus so try explicitly calling QWidget::setFocusPolicy with Qt::NoFocus before calling QWidget::show().

Also, make sure you're not calling QWidget::activateWindow() on the window or any of its widgets at any point.

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