如何找到活动的PyQt窗口并将其移到前面

2024-09-29 19:05:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我是PyQt的新手。我一直在寻找如何找到我的PyQt应用程序的窗口,它目前是打开的,并把它放在前面。到目前为止,我只找到一个使用pywin32的例子(因此是windows特有的)。我想问一下,是否有一种独立于平台的方式来实现这一目标。任何帮助都将不胜感激。在

这是我的密码。activateWindow()函数应该将它放在最前面。在

class TestApp(QtGui.QApplication):
    def __init__(self, argv, key):
        QtGui.QApplication.__init__(self, argv)
        self._activationWindow=None
        self._memory = QtCore.QSharedMemory()
        self._memory.setKey(key)
        if self._memory.attach():
            self._running = True
        else:
            self._running = False
            if not self._memory.create(1):
                raise RuntimeError(
                self._memory.errorString().toLocal8Bit().data())
    def isRunning(self):
        return self._running

    def activationWindow(self):
        return self._activationWindow

    def setActivationWindow(self, activationWindow):
        self._activationWindow = activationWindow

    def activateWindow(self):
        if not self._activationWindow:
            return
        self._activationWindow.setWindowState( self._activationWindow.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive)
        self._activationWindow.raise_()
        self._activationWindow.show()
        self._activationWindow.activateWindow()

Tags: keyselfreturnifinitdefrunningpyqt
1条回答
网友
1楼 · 发布于 2024-09-29 19:05:31

一个完整的、不依赖于平台的解决方案可能是遥不可及的。Qt支持的每个平台都有不同的行为方式,activateWindow似乎有点缺陷。在

首先,Qt文档对activateWindow的描述如下:

This function performs the same operation as clicking the mouse on the title bar of a top-level window. On X11, the result depends on the Window Manager. If you want to ensure that the window is stacked on top as well you should also call raise(). Note that the window must be visible, otherwise activateWindow() has no effect.

以及:

On Windows, if you are calling this when the application is not currently the active one then it will not make it the active window. It will change the color of the taskbar entry to indicate that the window has changed in some way. This is because Microsoft does not allow an application to interrupt what the user is currently doing in another application.

要获得更多关于困难的确凿证据,请查看Qt论坛上的以下帖子:

相关问题 更多 >

    热门问题