激活窗口
我有一个 QMainWindow。 它有这个参数:
this->setWindowFlags(Qt::Tool); this->setFocusPolicy(Qt::StrongFocus); this->setAttribute(Qt::WA_QuitOnClose,true);
showEvent 调用后,我的窗口已显示但未激活。 我尝试重载显示功能:
...
QMainWindow::showEvent(event);
this->activateWindow();
...
但这对我没有帮助。
编辑: 当我评论行时,
this->setWindowFlags(Qt::Tool);
一切正常,但我需要工具标志。 有任何想法吗?
编辑:
- 操作系统:Linux
- 编程语言:c++
- Qt 版本:4.5.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Windows 管理器决定
在开始之前:正如 elcuco 和 Javier,焦点策略和窗口布局的其他方面(例如标题bar) 属于相应窗口管理器的实质性扩展,Qt 的控制可能有限。 要看到这一点,只需查看具有“焦点跟随鼠标< /a>”政策。 在这些情况下,窗口管理器可能会忽略 Qt 的焦点请求。 由于这个原因,Qt 文档将许多相应的标志称为“提示”。 因此,某些建议的解决方案可能适合您,也可能不适合您。
QApplication::setActiveWindow()
尽管如此,e.tadeu 的 使用
QApplication::setActiveWindow() 的解决方案
对我来说适用于 Windows 和带有 Gnome 的 Ubuntu。 我用下面的代码测试了它。 抱歉,它是使用 PyQt 的 Python(我使用这样的问题来了解一些有关 PyQt 的知识)。 您应该很容易阅读它并将其翻译成 C++。请注意,您必须添加对 testWindow 的关闭事件的一些处理,因为如果您关闭 Qt::Tool 窗口,应用程序不会自动退出。
grabKeyboard() Hack
如果这对您不起作用,下面的 hack 可能会起作用。 我假设您的应用程序中有一个处于活动状态的窗口。 然后,您可以使用
grabKeyboard()
重定向输入。Qt::Tool
窗口没有获得焦点,但接收输入。 下面主要代码进行演示(其他代码不变)。基本上,当窗口
testWindow2
是活动窗口时,输入的所有文本都会显示在testWindow.textEdit
中。 我知道这不太好...创建自己的窗口
通过推出自己的窗口布局,您可以获得最大的灵活性(并为自己创造最多的工作)。 以下常见问题解答中描述了该想法。
其他“解决方案”
您可以直接调用相应窗口管理器的 API 函数来获得所需的结果(显然违背了首先使用 Qt 的原因)。 您还可以破解 Qt 源代码。 例如,在 Windows 上,Qt 使用
ShowWindow()
函数带有SW_SHOWNOACTIVATE
标志,用于显示样式为WS_EX_TOOLWINDOW
如果您设置了Qt::Tool
标志。 您可以轻松地将SW_SHOWNOACTIVATE
替换为您想要的任何内容。 Linux应该是一样的。 显然也不推荐。The Windows Manager Decides
Before I start: As pointed out by elcuco and Javier, focus policy and other aspects of the windows layout (e.g. the title bar) belongs to a substantial extend to the respective windows manager, and Qt might have limited control. To see this, just look at a user interface that has a "focus follows mouse" policy. In these cases, the windows manager might ignore Qt's focus request. For this reasons, the Qt documentation calls many of the respective flags "hints". Consequently, some of the suggested solutions might or might not work for you.
QApplication::setActiveWindow()
This not withstanding, e.tadeu's solution to use
QApplication::setActiveWindow()
works for me for both Windows and Ubuntu with Gnome. I tested it with the following code. Apologies that it is Python using PyQt (I use questions like these to learn a bit about PyQt). It should be fairly easy for you to read it and translate it into C++.Note that you have to add some handling of the close event of the
testWindow
, because the app does not exit automatically if you close aQt::Tool
window.The grabKeyboard() Hack
If this does not work for you, the following hack might. I assume that you have a window in your application that is active. You can then use
grabKeyboard()
to redirect the input. TheQt::Tool
window doesn't get the focus, but receives the input. The following main code demonstrates it (the other code remains unchanged).Basically, while the window
testWindow2
is the active one, all text entered shows up intestWindow.textEdit
. It is not nice, I know...Creating Your Own Window
You gain the most flexibility (and create the most work for yourself) by rolling out your own window layout. The idea is described in the following FAQ.
Other "Solutions"
You could directly call the respective window manager's API function to get the desired result (clearly against the very reason for using Qt in the first place). You could also hack the Qt source code. For example, on Windows, Qt uses the
ShowWindow()
function with aSW_SHOWNOACTIVATE
flag, to show a window with styleWS_EX_TOOLWINDOW
if you set theQt::Tool
flag. You could easily replace theSW_SHOWNOACTIVATE
with whatever you want. Linux should be the same. Clearly also not recommended.尝试使用 QApplication::setActiveWindow()
Try to use QApplication::setActiveWindow()
最初的苹果人机界面指南(*)表示工具箱窗口“始终位于顶部但从未激活”。 它还建议不要在它们上使用文本框,正是因为缺乏激活状态反馈。
检查其他“工具箱重”应用程序的行为方式。 我依稀记得至少GIMP和InkScape在这方面看起来很不一样。
正如elcuco所说,窗口管理器可以使用Qt的标志做任何它想做的事情。 而且,在 KDE、Gnome、fluxbox 等下肯定会有所不同。
(*):很棒的文件! 有点过时了; 但工具窗口已经被使用和考虑
The original apple Human Interface Guidelines(*) said that toolbox windows were "always on top but never activated". It also advises against using text boxes on them, precisely because the lack of activation-state feedback.
Check how other 'toolbox heavy' apps behave. I faintly recall that at least GIMP and InkScape seem very different in this aspect.
As elcuco said, the window manager can do whatever it wants with Qt's flags. Also, it sure would be different under KDE, Gnome, fluxbox, whatever.
(*):great document! somewhat outdated; but tool windows were already used and considered
哪个操作系统? 哪个Qt4?
在 Linux 上你注定失败,窗口管理器可以忽略你告诉他的内容。 请继续考虑。
Which operating system? Which Qt4?
On Linux you are doomed, the window manager can ignore what you are telling him. Keep it under consideration.
如果你只是将其设为常规 QWidget 而不是 QMainWindow,是否会发生同样的事情?
另外,也许您可能会更好地尝试通过其他方式来实现您需要 Qt::Tool 的任何效果(如果可能的话)?
Does the same thing happen if you just make it a regular QWidget instead of a QMainWindow?
Also, perhaps you might be better trying to achieve whatever effect you need Qt::Tool for by other means if that's possible?
关于Qt::Tool WindowFlags 引用Qt文档
似乎标志是一个问题,使用 Qt::WindowStaysOnTopHint 应该可以解决您的问题。
Regarding Qt::Tool WindowFlags To quote Qt documentation
It seems the flags is a problem and using Qt::WindowStaysOnTopHint should solve your problem.
只需在
setWindowFlags()
之后调用show()
即可。Just call
show()
aftersetWindowFlags()
.