PyQT 无法对 QTextCursor 进行排队 - 如何正确更改 window 内容
PyQT Cannot queue QTextCursor - how to properly change window content
再次长话短说。我正在编写 python 库,用于将非常简单的文本输出到 Qt 的 QTextBrowser window(存储在 window.ui 中)。据我之前发现,不应直接访问 Qt 对象。那么,什么是摆脱的正确方法:
QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)
当 'printing' 来自其他线程时出错?
# -*- 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_())
请明确地告诉我,因为我在阅读有关这些插槽和 Qt theads 的文章时完全被困住了并且感到困惑。
window.ui link 在这里:https://yadi.sk/d/U3esbMcIkvbFc
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, SIGNAL("print"), self.real_print)
再次长话短说。我正在编写 python 库,用于将非常简单的文本输出到 Qt 的 QTextBrowser window(存储在 window.ui 中)。据我之前发现,不应直接访问 Qt 对象。那么,什么是摆脱的正确方法:
QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)
当 'printing' 来自其他线程时出错?
# -*- 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_())
请明确地告诉我,因为我在阅读有关这些插槽和 Qt theads 的文章时完全被困住了并且感到困惑。
window.ui link 在这里:https://yadi.sk/d/U3esbMcIkvbFc
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, SIGNAL("print"), self.real_print)