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

kar*_*ana 8 python qt multithreading pyqt4

我试图从 PyQt 中的非主线程发送信号,但我不知道做错了什么!当我执行该程序时,它失败并出现以下错误:

QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

 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()
Run Code Online (Sandbox Code Playgroud)

我认为我将参数发送到线程的方式是错误的...我需要知道如何将参数发送到线程,在我的情况下,我需要发送q到工作线程。

mik*_*ent 6

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

要做的事情是使用信号/槽机制从工作线程发送任何类型的数据(在我的例子中通过扩展创建的线程QtCore.QRunnable,另一种模式显然是QtCore.QThreadQtCore.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)
Run Code Online (Sandbox Code Playgroud)

无意中直接从非“应用程序线程”调用它太容易了。

犯这样的错误然后引发异常是好的,因为它会为您提供显示罪魁祸首调用的堆栈跟踪。然后更改调用,以便它向 GUI 类发送一个信号,该信号的槽可以是 GUI 类中的方法(此处append_message),也可以是随后调用 的方法append_message

在我的示例中,我在上面添加了“滚动到底部”行,因为只有当我添加该行时,这些“无法排队”错误才开始发生。换句话说,完全有可能摆脱一定量的不合规处理(在这种情况下,每次调用都会添加更多文本),而不会引发任何错误......只有稍后您才会遇到困难。为了防止这种情况,我建议具有 GUI 功能的 GUI 类中的每个方法都应该包含这样的检查!

使用方法装饰器强制执行线程合规性

我现在(2023 年)将其用于 PyQt 项目中的大多数方法。任何失败都会显示调用堆栈(实际上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
Run Code Online (Sandbox Code Playgroud)

用法:

@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):
    ...
Run Code Online (Sandbox Code Playgroud)

请注意,假设这三个中的最后一个实际上没有检查这种合规性是没有意义的,这是错误的。Qt 失败因未捕获异常而臭名昭著,因为实际上没有任何东西检查引发的异常!这将捕获所有 BaseExceptions 并记录一个很好的调用堆栈跟踪。否则,您必须将每个方法的内容括在 中try ... except。但请注意,它实际上不会捕获 C++ 代码中抛出/引发的所有 Qt 异常,但它通常会为令人困惑的崩溃提供大量指导。


han*_*ank 0

确保使用 qRegisterMetaType() 注册“QTextCursor”。

您尝试过使用qRegisterMetaType函数吗?

官方手册

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

  • OP 使用 PyQt,它不包装 `qRegisterMetaType`。 (12认同)