问题描述
我试图从 PyQt 中的非主线程发送信号,但我不知道做错了什么!当我执行程序时,它失败并显示此错误:
Im trying to send a signal from a non-main thread in PyQt but i dont know what am doing wrong! And when i execute the program it fails with this error:
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
发送到工作线程.
I think that my way of send parameter to the thread is wrong...I need to know how to send parameters to a thread, in my case i need to send q
to the worker thread.
推荐答案
您是否尝试使用 qRegisterMetaType
函数?
Did you try to use qRegisterMetaType
function?
官方手册说:
该类用作在 QVariant 和排队的信号和插槽连接.它将类型名称关联到类型,以便它可以在运行时动态创建和销毁.使用 Q_DECLARE_METATYPE() 声明新类型以使其可用于QVariant 和其他基于模板的函数.调用 qRegisterMetaType()使类型可用于非基于模板的函数,例如排队的信号和槽连接.
这篇关于QObject::connect: 无法排队“QTextCursor"类型的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!