Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQT无法将QTextCursor排入队列-如何正确更改窗口内容_Python_Pyqt_Pyqt4 - Fatal编程技术网

Python PyQT无法将QTextCursor排入队列-如何正确更改窗口内容

Python PyQT无法将QTextCursor排入队列-如何正确更改窗口内容,python,pyqt,pyqt4,Python,Pyqt,Pyqt4,长话短说。我正在编写python库,用于将非常简单的文本输出到Qt的QTextBrowser窗口(存储在window.ui中)。就我以前所发现的,不应该直接访问Qt对象。那么,正确的摆脱方法是什么: QObject::connect: Cannot queue arguments of type 'QTextCursor' (Make sure 'QTextCursor' is registered using qRegisterMetaType().) 从其他线程“打印”时出错 # -*-

长话短说。我正在编写python库,用于将非常简单的文本输出到Qt的QTextBrowser窗口(存储在window.ui中)。就我以前所发现的,不应该直接访问Qt对象。那么,正确的摆脱方法是什么:

QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)
从其他线程“打印”时出错

# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore, uic
import sys
import time


def async(func):
    from threading import Thread
    from functools import wraps

    @wraps(func)
    def async_func(*args, **kwargs):
        func_hl = Thread(target=func, args=args, kwargs=kwargs)
        func_hl.start()
        return func_hl
    return async_func


class GuiFile(QObject):   # add also GUI_FORM
    def __init__(self, filename):
        QObject.__init__(self, None)
        self.filename = filename
        self.myapp = sys.argv
        self.app = QtGui.QApplication(self.myapp)

    def show(self):
        self.window = uic.loadUi(self.filename)
        self.window.show()


    def pprint(self, text):
        gui_object = self.window.outbox
        gui_object.insertHtml(text + '<br>')
        gui_object.moveCursor(QtGui.QTextCursor.End)

if __name__ == '__main__':

    @async
    def other_thead():
        time.sleep(1)
        for x in xrange(3):
            print x
            gui.pprint(u'Hello %s' % x)

    gui = GuiFile('window.ui')
    gui.show()
    other_thead()
    sys.exit(gui.app.exec_())
#-*-编码:utf-8-*-
从PyQt4导入QtGui、QtCore、uic
导入系统
导入时间
def异步(func):
从线程导入线程
从functools导入包装
@包装(func)
定义异步函数(*args,**kwargs):
func_hl=Thread(target=func,args=args,kwargs=kwargs)
功能启动()
返回函数
返回异步函数
类GUI文件(QObject):#还添加GUI表单
def uuu init uuu(self,文件名):
QObject.\uuuuu初始化(self,None)
self.filename=文件名
self.myapp=sys.argv
self.app=QtGui.QApplication(self.myapp)
def显示(自我):
self.window=uic.loadUi(self.filename)
self.window.show()
def pprint(自我,文本):
gui\u object=self.window.outbox
gui_object.insertHtml(文本+“
”) gui_object.moveCursor(QtGui.QTextCursor.End) 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': @异步的 def other_thead(): 时间。睡眠(1) 对于x范围内的x(3): 打印x gui.pprint(u'Hello%s'%x) gui=GuiFile('window.ui') gui.show() 其他附件() sys.exit(gui.app.exec)
请明确地告诉我,‘因为我完全被那些插槽和Qt-Thead卡住了,弄糊涂了

window.ui链接位于此处:

upd1:

我也试着发出这样的信号:

    self.connect(self.window.outbox, SIGNAL("print"), self.real_print)


    def real_print(self, text, **kwargs):
        print 'RP'
        gui_object = self.window.outbox

        """ What if we print to other field? """

        if kwargs.get('field'):
            field = kwargs['field']

            for elem in dir(self.window):
                if str(elem) == field:
                    gui_object = getattr(self.window, elem)

        gui_object.insertHtml(text + '<br>')
        gui_object.moveCursor(QTextCursor.End)

    def pprint(self, text, **kwargs):
        self.emit(SIGNAL("print"), text)
self.connect(self.window.outbox、信号(“打印”)、self.real\u打印)
def real_打印(自身、文本、**kwargs):
打印“RP”
gui\u object=self.window.outbox
“”“如果我们打印到其他字段怎么办?”“”
如果kwargs.get('field'):
field=kwargs['field']
对于目录中的元素(自窗口):
如果str(elem)=字段:
gui\u object=getattr(self.window,elem)
gui_object.insertHtml(文本+“
”) gui_object.moveCursor(QTextCursor.End) def pprint(自我,文本,**kwargs): 自发光(信号(“打印”),文本)

但它仍然不起作用。

哦,天哪,它解决了

        self.connect(self, SIGNAL("print"), self.real_print)