PyQt-QObject::connect: Cannot queue arguments...報錯


【問題】

源碼:

class Windows(QtGui.QWidget):     # Creates a widget containing:     # - a QLineEdit (status_widget)     # - a button, connected to on_run_clicked     def on_run_clicked(self):         def update(text):             self.widget.setText(text)         threading.Thread(target=run, args=(update, )).start()


但是如果將 QLineEdit換成QTextEdit,並使用了append方法就會得到以下錯誤:

QObject::connect: Cannot queue arguments of type 'QTextCursor'

(Make sure 'QTextCursor' is registered using qRegisterMetaType().)

 

【解決】

我們不能通過線程來修改UI,較為安全的修改用戶界面的方式是向UI窗口發送信號(signal),較為簡單的方式是使用 Qt threading

class MyThread(QtCore.QThread):     updated = QtCore.pyqtSignal(str)     def runself ):         # do some functionality         for i in range(10000):             self.updated.emit(str(i)) class Windows(QtGui.QWidget):     def __init__self, parent = None ):         super(Windows, self).__init__(parent)         self._thread = MyThread(self)         self._thread.updated.connect(self.updateText)         # create a line edit and a button         self._button.clicked.connect(self._thread.start)     def updateTextself, text ):         self.widget.setText(text)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM