QObject :: connect:无法对“QTextCursor”类型的参数进行排队

时间:2012-12-02 22:49:03

标签: python multithreading qt pyqt4

我试图从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发送到工作线程。

2 个答案:

答案 0 :(得分:3)

  

确保使用qRegisterMetaType()注册'QTextCursor'。

您是否尝试使用qRegisterMetaType功能?

官方手册says

  

该类用作帮助编组QVariant和in中的类型   排队的信号和插槽连接。它将类型名称与a关联   键入,以便可以在运行时动态创建和销毁它。   使用Q_DECLARE_METATYPE()声明新类型以使其可用   QVariant和其他基于模板的功能。 调用qRegisterMetaType()   使类型可用于非基于模板的函数,例如   排队的信号和插槽连接

答案 1 :(得分:0)

PyQt5 非常新,但是当您尝试从不是“应用程序线程”的线程执行 GUI 操作时,似乎会发生这种情况。我将其放在引号中,因为认为即使在相当简单的 PyQt5 应用程序中,QApplication.instance().thread() 也将始终返回相同的对象似乎是错误的。

要做的是使用信号/槽机制从工作线程(在我的例子中是通过扩展 QtCore.QRunnable 创建的线程,尽管可能有其他模式)发送任何类型的数据。

然后还要检查所有可能从非“应用程序线程”接收数据的槽方法。在执行期间可视化记录消息的示例:

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 类中的每个方法都应该包含这样的检查!