Make sure 'QTextCursor' is registered using qRegisterMetaType()
Unsolved
Qt for Python
-
Im sure you guys get this alot but im trying to thread and im getting this error:
(Parent is QTextDocument(0x1243bfd4290), parent's thread is QThread(0x12437dd1260), current thread is myThread(0x1243c008470) QObject::connect: Cannot queue arguments of type 'QTextCursor' (Make sure 'QTextCursor' is registered using qRegisterMetaType().)``` Can anyone help? My thread: ```class myThread(QThread): def __init__(self, username, _previous, text): super().__init__() self.username = username self._previous = _previous self.text = text print(dir(QThread)) def run(self): n = len([k for k in _MESSAGE_COLLECTION.find({'msg': True})]) while True: if n != len([k for k in _MESSAGE_COLLECTION.find({'msg': True})]): self.message = _MESSAGE_COLLECTION.find_one({'_id': _ID + n}) self.execute_update() n += 1 def execute_update(self): if self.message['from'] != self.username: self._previous.append(f'\n\n{self.message["from"]} says: {self.message["message"]}') self.text.setPlainText("\n".join(self._previous[::-1]))``` and i call it with
self.thread = myThread(self.username, self._previous, self.text)
self.thread.start()``` -
@Caeden You should not directly modify the GUI in another thread.
class myThread(QThread): text_changed = Signal(str) def __init__(self, username, _previous): super().__init__() self.username = username self._previous = _previous def run(self): n = len([k for k in _MESSAGE_COLLECTION.find({"msg": True})]) while True: if n != len([k for k in _MESSAGE_COLLECTION.find({"msg": True})]): self.message = _MESSAGE_COLLECTION.find_one({"_id": _ID + n}) self.execute_update() n += 1 def execute_update(self): if self.message["from"] != self.username: self._previous.append( f'\n\n{self.message["from"]} says: {self.message["message"]}' ) self.text_changed.emit("\n".join(self._previous[::-1])) QThread.msleep(10)
self.my_thread = myThread(self.username, self._previous) self.my_thread.text_changed.connect(self.text.setPlainText) self.my_thread.start()
-
@eyllanesc ill try this, thank you!
-
@eyllanesc how can i pass variables through the emit? So i'm doing
self.thread = myThread(self.username, self._previous, self.text) self.myThread.text_changed.connect(self.settext) self.thread.start()
and
def settext(self):
but i dont know how i can pass the
_message
-
@Caeden It seems that you have not understood the usefulness or operation of the signals, I recommend you review the official docs. what is
_message
?If _message is a string then the slot must have the argument:
foo_text = "Foo" self.text_changed.emit(foo_text)
def settext(self, text): print(text)