【问题标题】:How to add your own loop to Qt?如何将自己的循环添加到 Qt?
【发布时间】:2016-12-05 21:14:30
【问题描述】:

我不知道该怎么做。当我的 Qt 应用程序不在焦点时,我有一个 while 循环供 xlib 读取按键。这是 ubuntu 16.04 python3-xlib 包 /usr/share/doc/python3-xlib/examples/record_demo.py 的复制粘贴。但这破坏了 Qt 应用程序的功能,因为它只读取按键而不处理 Qt 的事件循环。我应该让我的 Qt 应用程序为此设置第二个线程吗?或者我可以将 xlib 的循环挂接到 Qt 的循环上吗?

我正在使用 python3、pyqt5 和 python3-xlib。

下面是文件/usr/share/doc/python3-xlib/examples/record_demo.py

#!/usr/bin/python3
#
# examples/record_demo.py -- demonstrate record extension
#
#    Copyright (C) 2006 Alex Badea <vamposdecampos@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Simple demo for the RECORD extension
# Not very much unlike the xmacrorec2 program in the xmacro package.

import sys
import os

# Change path so we find Xlib
sys.path.insert(1, os.path.join(sys.path[0], '..'))

from Xlib import X, XK, display
from Xlib.ext import record
from Xlib.protocol import rq

local_dpy = display.Display()
record_dpy = display.Display()

def lookup_keysym(keysym):
    for name in dir(XK):
        if name[:3] == "XK_" and getattr(XK, name) == keysym:
            return name[3:]
    return "[%d]" % keysym

def record_callback(reply):
    if reply.category != record.FromServer:
        return
    if reply.client_swapped:
        print("* received swapped protocol data, cowardly ignored")
        return
    if not len(reply.data) or reply.data[0] < 2:
        # not an event
        return

    data = reply.data
    while len(data):
        event, data = rq.EventField(None).parse_binary_value(data, record_dpy.display, None, None)

        if event.type in [X.KeyPress, X.KeyRelease]:
            pr = event.type == X.KeyPress and "Press" or "Release"

            keysym = local_dpy.keycode_to_keysym(event.detail, 0)
            if not keysym:
                print("KeyCode%s %s" % (pr, event.detail))
            else:
                print("KeyStr%s %s" % (pr, lookup_keysym(keysym)))

            if event.type == X.KeyPress and keysym == XK.XK_Escape:
                local_dpy.record_disable_context(ctx)
                local_dpy.flush()
                return
        elif event.type == X.ButtonPress:
            print("ButtonPress %s" % event.detail)
        elif event.type == X.ButtonRelease:
            print("ButtonRelease %s" % event.detail)
        elif event.type == X.MotionNotify:
            print("MotionNotify %i %i" % (event.root_x, event.root_y))


# Check if the extension is present
if not record_dpy.has_extension("RECORD"):
    print("RECORD extension not found")
    sys.exit(1)
r = record_dpy.record_get_version(0, 0)
print("RECORD extension version %d.%d" % (r.major_version, r.minor_version))

# Create a recording context; we only want key and mouse events
ctx = record_dpy.record_create_context(
        0,
        [record.AllClients],
        [{
                'core_requests': (0, 0),
                'core_replies': (0, 0),
                'ext_requests': (0, 0, 0, 0),
                'ext_replies': (0, 0, 0, 0),
                'delivered_events': (0, 0),
                'device_events': (X.KeyPress, X.MotionNotify),
                'errors': (0, 0),
                'client_started': False,
                'client_died': False,
        }])

# Enable the context; this only returns after a call to record_disable_context,
# while calling the callback function in the meantime
record_dpy.record_enable_context(ctx, record_callback)

# Finally free the context
record_dpy.record_free_context(ctx)

【问题讨论】:

  • 每个 GUI 框架都有一些 Timer 类,它可以让你定期执行函数,然后你可以使用它来代替 while True 或其他循环。
  • 我会说你需要的一切都是 QThread 和重新实现的 run 方法。在 run 方法中打包你的 while 循环,并在需要时启动线程。
  • 我不确定该演示代码是否具有库已经自动记录所有按键并使用循环简单地打印出它们的队列。
  • 显然使用单独的线程在不同于主 Qt GUI 线程的线程上运行 xlib 代码是非常糟糕的。

标签: python qt pyqt xlib


【解决方案1】:

Qt 的事件循环一个 X 事件循环。你不需要另一个。

由于您从事件循环中以回调的形式获取信息,这将“正常工作”:只要您使用与 Qt 相同的 xcb 库,并在那里注册回调,您的回调就会被调用.

如果您以事件的形式获取信息,您可以过滤所有到达您的应用程序的本机 X 事件:使用 QCoreApplication::installNativeEventFilter。您对QAbstractNativeEventFilter 的具体实现将接收本地事件作为指向xcb_generic_event_t 的指针,请参阅the documentation

不过,如果发生事件,与 Python 的互操作会有点复杂。您可能需要在 C++ 中实现事件过滤器并将其公开给 Python。

【讨论】:

  • 这不是只有在 Qt 应用程序处于焦点时才有效,因为在应用程序未处于焦点时不会生成任何事件?当 Qt 应用程序不在焦点时,我正在尝试读取键。
  • 你提到的 API 在 PyQt 中是受支持的。但是,当应用程序 没有 具有焦点(这是 OP 感兴趣的)时,它只会捕获修饰键按下。所以我认为这不会有太大帮助。
  • @user1766555。我认为最简单的解决方案可能是使用单独的线程并将信号发送回主线程。
  • @ekhumoro 我也在想这个,开始变得头疼。我找不到任何提供 xcb 支持的 python 3 包。我想我会为 xlib 循环使用第二个线程来检查是否按下了快捷方式。
  • 我放弃了制作键盘快捷键阅读器的尝试。 Python 3.5.2 对 XCB 非常不友好,我遇到了太多的车辙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
  • 2020-02-16
  • 2021-04-08
  • 2019-04-26
  • 2015-07-16
  • 2021-06-13
相关资源
最近更新 更多