我试图发送一个信号,从一个非主线程在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
发送到工作线程。
发布于 2021-05-22 18:37:04
对于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功能的每个方法都应该包括这样的检查!
发布于 2012-12-03 08:55:09
确保“QTextCursor”是使用qRegisterMetaType()注册的。
你试过使用qRegisterMetaType
函数吗?
官方手册他说
该类用于辅助QVariant中的类型以及队列信号和时隙连接中的类型。它将类型名称与类型关联起来,以便能够在运行时动态地创建和销毁它。使用Q_DECLARE_METATYPE()声明新类型,使其可用于QVariant和其他基于模板的函数。调用qRegisterMetaType()使基于非模板的函数(如排队信号和时隙连接)可以使用类型。
发布于 2022-07-11 19:51:48
我想在@Mike啮齿动物的帖子中添加以下注释,它解决了我的问题(我正在使用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)
https://stackoverflow.com/questions/13674792
复制相似问题