如何为 X11 提供 Qt4 窗口的 wm_name?

发布于 2024-11-10 16:35:58 字数 2851 浏览 2 评论 0原文

背景

我即将解决另一个问题,包括为 X11 上的 Qt 窗口保留屏幕空间。为此,我使用 PyQt4Python-Xlib.

情况

我想要节省屏幕空间的应用程序是一个无框架的 Qt 窗口,25px 高和屏幕宽,它实际上是一个面板。简化的代码如下所示:

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
# this module i made myself (see below):
import myXwindow

class QtPanel(QtGui.QWidget):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        # here i explicitely name the window:
        self.setWindowTitle('QtPanel')
        self.resize(QtGui.QDesktopWidget().screenGeometry().width(), 25)
        self.move(0,0)
        self.setWindowFlags(QtCore.Qt.Widget |
        QtCore.Qt.FramelessWindowHint |
        QtCore.Qt.WindowStaysOnTopHint)
        self.setAttribute(QtCore.Qt.WA_X11NetWmWindowTypeDock)

    def main():
        app = QtGui.QApplication(sys.argv)
        panel = QtPanel()
        panel.show() 
        xwindow = myXwindow.Window(str(panel.windowTitle()))
        sys.exit(app.exec_())

if __name__ == '__main__':
    main()

现在要为此 QtPanel 应用程序保留空间,我需要调用 xlib。下面我将仅展示我打算如何从 X 中“抓取”窗口。在上面的代码中导入以下模块以获取面板的 Xwindow:

from Xlib.display import Display
from Xlib import X

class Window(object):

    def __init__(self, title):

        self._title = title
        self._root = Display().screen().root
        self._window = self.find_window()

    def find_window(self):

        my_window = None
        display = Display()

        windowIDs = self._root.get_full_property(
                    display.intern_atom('_NET_CLIENT_LIST'),
                    X.AnyPropertyType).value

        print 'looking for windows:'
        count = 0
        for windowID in windowIDs:
            count += 1
            window = display.create_resource_object('window', windowID)
            title = window.get_wm_name()
            print 'window', count, ':', title
            if self._title in title:
                my_window = window

        return my_window

问题

现在当我运行 QtPanel 应用程序时,它应该返回一个列表当前由 X 显示的所有窗口的名称。尽管已在代码中明确命名(请参见上文),QtPanel 应用程序要么没有名称(即 None 或 ''),要么被命名为 'x-nautilus-desktop ',这里是返回内容的示例:

$ python ./Qtpanel.py
looking for windows:
window 1 : Bottom Expanded Edge Panel
window 2 : cairo-dock
window 3 : x-nautilus-desktop
window 4 : ReserveSpace - NetBeans IDE 6.9
window 5 : How to provide X11 with a wm_name for a Qt4 window? - Stack Overflow - Mozilla Firefox

问题

How can i 'name' my Qt Application or Qt toplevel window so it shown on X? 和/或:除了应用程序名称之外,我如何在 X 中识别我的应用程序?

Background

I am about to resolve another issue, which consists in reserving screen-space for a Qt Window on X11. For this i use PyQt4 and Python-Xlib.

Situation

The application i want to save screen space for is a frameless Qt Window, 25px high and screen-wide, it's a panel in fact. The simplified code looks like this:

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
# this module i made myself (see below):
import myXwindow

class QtPanel(QtGui.QWidget):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        # here i explicitely name the window:
        self.setWindowTitle('QtPanel')
        self.resize(QtGui.QDesktopWidget().screenGeometry().width(), 25)
        self.move(0,0)
        self.setWindowFlags(QtCore.Qt.Widget |
        QtCore.Qt.FramelessWindowHint |
        QtCore.Qt.WindowStaysOnTopHint)
        self.setAttribute(QtCore.Qt.WA_X11NetWmWindowTypeDock)

    def main():
        app = QtGui.QApplication(sys.argv)
        panel = QtPanel()
        panel.show() 
        xwindow = myXwindow.Window(str(panel.windowTitle()))
        sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Now to reserve space for this QtPanel application, i need to call on xlib. Here below i will just show how i intend to 'grab' my window from X. The following module is imported in the code here above to get the Xwindow of my panel:

from Xlib.display import Display
from Xlib import X

class Window(object):

    def __init__(self, title):

        self._title = title
        self._root = Display().screen().root
        self._window = self.find_window()

    def find_window(self):

        my_window = None
        display = Display()

        windowIDs = self._root.get_full_property(
                    display.intern_atom('_NET_CLIENT_LIST'),
                    X.AnyPropertyType).value

        print 'looking for windows:'
        count = 0
        for windowID in windowIDs:
            count += 1
            window = display.create_resource_object('window', windowID)
            title = window.get_wm_name()
            print 'window', count, ':', title
            if self._title in title:
                my_window = window

        return my_window

Problem

Now when i run the QtPanel application, it should return a list of names of all the windows currently displayed by X. Despite having been named explicitly in the code (see here above) the QtPanel application either has no name (i.e. None or '') or is named 'x-nautilus-desktop', here is a sample of what was returned:

$ python ./Qtpanel.py
looking for windows:
window 1 : Bottom Expanded Edge Panel
window 2 : cairo-dock
window 3 : x-nautilus-desktop
window 4 : ReserveSpace - NetBeans IDE 6.9
window 5 : How to provide X11 with a wm_name for a Qt4 window? - Stack Overflow - Mozilla Firefox

Question

How can i 'name' my Qt Application or Qt toplevel window so it shows properly on X?
And/or: how can i identify my application in X other than with the application's name?

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

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

发布评论

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

评论(1

远昼 2024-11-17 16:35:59

虽然我不太知道未命名的窗口是什么(它似乎与启动 QtPanel 应用程序有关,但 Xlib 脚本无法找到显示的窗口,因为它是在事件循环 QApplication._exec() 之前查询的。可能需要线程在主循环之外执行此操作。

Although i don't quite know what the unnamed window is (and it seems related to launching the QtPanel application, the Xlib script is not able to find the window on display because it is queried before the event loop QApplication._exec(). A thread may be required to do that outside of the main loop.

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