激活窗口

发布于 2024-07-25 10:21:20 字数 628 浏览 8 评论 0 原文

我有一个 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

i have a QMainWindow. It has this parameters:

this->setWindowFlags(Qt::Tool);
this->setFocusPolicy(Qt::StrongFocus);
this->setAttribute(Qt::WA_QuitOnClose,true);

After showEvent calles my window is shown but unactivated.
I tried to overload show function:

...    
QMainWindow::showEvent(event);
this->activateWindow();
...

But it doesn't help me.

EDIT:
When i commented line

this->setWindowFlags(Qt::Tool);

everything worked fine, but i need in tool-flag.
Any ideas?

EDIT:

  • OS: Linux
  • Programming language: c++
  • Qt version: 4.5.1

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

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

发布评论

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

评论(7

披肩女神 2024-08-01 10:21:20

Windows 管理器决定

在开始之前:正如 elcucoJavier,焦点策略和窗口布局的其他方面(例如标题bar) 属于相应窗口管理器的实质性扩展,Qt 的控制可能有限。 要看到这一点,只需查看具有“焦点跟随鼠标< /a>”政策。 在这些情况下,窗口管理器可能会忽略 Qt 的焦点请求。 由于这个原因,Qt 文档将许多相应的标志称为“提示”。 因此,某些建议的解决方案可能适合您,也可能不适合您。

QApplication::setActiveWindow()

尽管如此,e.tadeu 的 使用 QApplication::setActiveWindow() 的解决方案 对我来说适用于 Windows 和带有 Gnome 的 Ubuntu。 我用下面的代码测试了它。 抱歉,它是使用 PyQt 的 Python(我使用这样的问题来了解一些有关 PyQt 的知识)。 您应该很容易阅读它并将其翻译成 C++。

import sys

from PyQt4 import QtGui
from PyQt4 import QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self)

        # main window
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Test')

        # text editor
        self.textEdit = QtGui.QTextEdit()
        self.setCentralWidget(self.textEdit)

    def closeEvent(self, event):
        QtGui.QApplication.instance().quit()

#main
app = QtGui.QApplication(sys.argv)
testWindow = MainWindow()
testWindow.setWindowFlags(QtCore.Qt.Tool)
testWindow.show()
app.setActiveWindow(testWindow)
app.exec_()

请注意,您必须添加对 testWindow 的关闭事件的一些处理,因为如果您关闭 Qt::Tool 窗口,应用程序不会自动退出。

grabKeyboard() Hack

如果这对您不起作用,下面的 hack 可能会起作用。 我假设您的应用程序中有一个处于活动状态的窗口。 然后,您可以使用 grabKeyboard() 重定向输入。 Qt::Tool 窗口没有获得焦点,但接收输入。 下面主要代码进行演示(其他代码不变)。

#main
app = QtGui.QApplication(sys.argv)
testWindow = MainWindow()
testWindow.setWindowFlags(QtCore.Qt.Tool)
testWindow2 = MainWindow()   # second window which is active
testWindow2.show()
testWindow.show()
testWindow.textEdit.grabKeyboard()
app.exec_()

基本上,当窗口 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++.

import sys

from PyQt4 import QtGui
from PyQt4 import QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self)

        # main window
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Test')

        # text editor
        self.textEdit = QtGui.QTextEdit()
        self.setCentralWidget(self.textEdit)

    def closeEvent(self, event):
        QtGui.QApplication.instance().quit()

#main
app = QtGui.QApplication(sys.argv)
testWindow = MainWindow()
testWindow.setWindowFlags(QtCore.Qt.Tool)
testWindow.show()
app.setActiveWindow(testWindow)
app.exec_()

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 a Qt::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. The Qt::Tool window doesn't get the focus, but receives the input. The following main code demonstrates it (the other code remains unchanged).

#main
app = QtGui.QApplication(sys.argv)
testWindow = MainWindow()
testWindow.setWindowFlags(QtCore.Qt.Tool)
testWindow2 = MainWindow()   # second window which is active
testWindow2.show()
testWindow.show()
testWindow.textEdit.grabKeyboard()
app.exec_()

Basically, while the window testWindow2 is the active one, all text entered shows up in testWindow.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 a SW_SHOWNOACTIVATE flag, to show a window with style WS_EX_TOOLWINDOW if you set the Qt::Tool flag. You could easily replace the SW_SHOWNOACTIVATE with whatever you want. Linux should be the same. Clearly also not recommended.

梦里兽 2024-08-01 10:21:20

尝试使用 QApplication::setActiveWindow()

Try to use QApplication::setActiveWindow()

赠我空喜 2024-08-01 10:21:20

最初的苹果人机界面指南(*)表示工具箱窗口“始终位于顶部但从未激活”。 它还建议不要在它们上使用文本框,正是因为缺乏激活状态反馈。

检查其他“工具箱重”应用程序的行为方式。 我依稀记得至少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

清引 2024-08-01 10:21:20

哪个操作系统? 哪个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.

能怎样 2024-08-01 10:21:20

如果你只是将其设为常规 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?

七分※倦醒 2024-08-01 10:21:20

关于Qt::Tool WindowFlags 引用Qt文档

表示该小部件是一个工具
窗户。 工具窗口通常很小
窗口的标题比平常小
酒吧和装饰,通常用于
工具按钮的集合。 它在那里
是父级,工具窗口将
始终保持在其之上。 如果有
不是父母,您可以考虑使用
Qt::WindowStaysOnTopHint 也是如此。 如果
windows系统支持它,一个工具
窗户可以装饰
框架稍微轻一些。 也可以是
与Qt::FramelessWindowHint结合

似乎标志是一个问题,使用 Qt::WindowStaysOnTopHint 应该可以解决您的问题。

Regarding Qt::Tool WindowFlags To quote Qt documentation

Indicates that the widget is a tool
window. A tool window is often a small
window with a smaller than usual title
bar and decoration, typically used for
collections of tool buttons. It there
is a parent, the tool window will
always be kept on top of it. If there
isn't a parent, you may consider using
Qt::WindowStaysOnTopHint as well. If
the window system supports it, a tool
window can be decorated with a
somewhat lighter frame. It can also be
combined with Qt::FramelessWindowHint

It seems the flags is a problem and using Qt::WindowStaysOnTopHint should solve your problem.

锦爱 2024-08-01 10:21:20

只需在 setWindowFlags() 之后调用 show() 即可。

Just call show() after setWindowFlags().

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