我试图从 PyQt 中的非主线程发送信号,但我不知道做错了什么!当我执行该程序时,它失败并出现以下错误:
QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)
这是我的代码:
class Sender(QtCore.QThread):
def __init__(self,q):
super(Sender,self).__init__()
self.q=q
def run(self):
while True:
pass
try: line = q.get_nowait()
# or q.get(timeout=.1)
except Empty:
pass
else:
self.emit(QtCore.SIGNAL('tri()'))
class Workspace(QMainWindow, Ui_MainWindow):
""" This class is for managing the whole GUI `Workspace'.
Currently a Workspace is similar to a MainWindow
"""
def __init__(self):
try:
from Queue import Queue, Empty
except ImportError:
while True:
#from queue import Queue, Empty # python 3.x
print "error"
ON_POSIX = 'posix' in sys.builtin_module_names
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
p= Popen(["java -Xmx256m -jar bin/HelloWorld.jar"],cwd=r'/home/karen/sphinx4-1.0beta5-src/sphinx4-1.0beta5/',stdout=PIPE, shell=True, bufsize= 4024)
q = Queue()
t = threading.Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True # thread dies with the program
t.start()
self.sender= Sender(q)
self.connect(self.sender, QtCore.SIGNAL('tri()'), self.__action_About)
self.sender.start()
我认为我向线程发送参数的方式是错误的...... 我需要知道如何将参数发送到线程,在我的例子中,我需要将
q
发送到工作线程。
对于 PyQt5 来说相当新,但是当您尝试从不是“应用程序线程”的线程执行 GUI 操作时,似乎会发生这种情况。我将其放在引号中,因为即使在相当简单的 PyQt5 应用程序中,认为
QApplication.instance().thread()
将始终返回相同的对象似乎是错误的。
要做的事情是使用信号/槽机制从工作线程发送任何类型的数据(在我的例子中通过扩展
QtCore.QRunnable
创建的线程,另一种模式显然是QtCore.QThread
和QtCore.QObject.moveToThread
,请参阅这里)。
然后还包括检查所有可能从非“应用程序线程”接收数据的插槽方法。在执行过程中以可视方式记录消息的示例:
def append_message(self, message):
# this "instance" method is very useful!
app_thread = QtWidgets.QApplication.instance().thread()
curr_thread = QtCore.QThread.currentThread()
if app_thread != curr_thread:
raise Exception('attempt to call MainWindow.append_message from non-app thread')
ms_now = datetime.datetime.now().isoformat(sep=' ', timespec='milliseconds')
self.messages_text_box.insertPlainText(f'{ms_now}: {message}\n')
# scroll to bottom
self.messages_text_box.moveCursor(QtGui.QTextCursor.End)
直接从非“应用程序线程”无意中调用它太容易了。
犯这样的错误然后引发异常是好的,因为它会为您提供显示罪魁祸首调用的堆栈跟踪。然后更改调用,以便它向 GUI 类发送一个信号,该信号的槽可以是 GUI 类中的方法(此处为
append_message
),或者也可以 依次 调用 append_message
。
在我的示例中,我在上面添加了“滚动到底部”行,因为只有当我添加该行时,这些“无法排队”错误才开始发生。换句话说,完全有可能摆脱一定量的不合规处理(在这种情况下,每次调用都会添加更多文本),而不会引发任何错误......只有稍后您才会遇到困难。为了防止这种情况,我建议具有 GUI 功能的 GUI 类中的每个方法都应该包含这样的检查!
使用方法装饰器强制执行线程合规性
我将其用于 PyQt 项目中的大多数方法。任何失败都会显示 calling 堆栈(实际上
logger.exception(...)
在这种情况下不起作用......)
# ideally, make your life easier and properly configure a non-root logger
logger = logging.getLogger()
...
def thread_check(gui_thread: bool):
def pseudo_decorator(func):
if not callable(func):
# coding error: decorator has been applied to something which is not a function
raise Exception(f'func is non-callable type {type(func)}')
def gui_checker_inner_function(*args, **kwargs):
try:
func.stack_trace = None
if QtWidgets.QApplication == None:
return None
if QtWidgets.QApplication.instance() != None:
app_thread = QtWidgets.QApplication.instance().thread()
curr_thread = QtCore.QThread.currentThread()
# None means we don't check the GUI status of the thread
if gui_thread != None:
if (curr_thread == app_thread) != gui_thread:
# NB WRONG THREAD! QT likely to crash soon afterwards if this happens ...
raise Exception(f'method {func.__qualname__} should have been called in {"GUI thread" if gui_thread else "non-GUI thread"}')
def executing_func(*args, **kwargs):
func.stack_trace = ''.join(traceback.format_stack())
thread_check.stack_trace = ''.join(traceback.format_stack())
thread_check.func = func
return func(*args, **kwargs)
return executing_func(*args, **kwargs)
except BaseException as e:
msg = f'stack trace:\n{func.stack_trace}\n'
if logger == None:
print(msg, file=sys.stderr)
else:
logger.exception(msg)
# NB a KeyboardInterrupt on the DOS screen from which the app was launched, will not be "seen"
# until you put focus back on the app (from the DOS screen)... but when you do this will stop the app:
if isinstance(e, KeyboardInterrupt):
sys.exit()
raise e
return gui_checker_inner_function
return pseudo_decorator
用途:
@thread_check(True)
def my_gui_method(self):
...
@thread_check(False)
def my_non_gui_method(self):
...
@thread_check(None)
def can_work_in_either_context(self):
...
确保使用 qRegisterMetaType() 注册“QTextCursor”。
您尝试过使用
qRegisterMetaType
功能吗?
官方手册说:
该类用作在 QVariant 和中编组类型的帮助器 排队的信号和槽连接。它将类型名称与 类型,以便可以在运行时动态创建和销毁它。 使用 Q_DECLARE_METATYPE() 声明新类型以使其可用于 QVariant 和其他基于模板的函数。 调用qRegisterMetaType() 使类型可用于非基于模板的函数,例如 排队的信号和槽连接。
我想在 @mike rodent 的帖子中添加以下注释来解决我的问题(我正在使用 PyQt5):
QObject::connect: Cannot queue arguments of type 'QTextCursor'
消息,我需要找到以下位置并添加一些代码:
class_attribute = pyqtSignal(str)
这样的东西。self.class_attribute.connect(self.slot_name)
self.class_attribute.emit(str)