QObject::setParent: Cannot set parent, new parent is in a different thread Segmentation fault
-
i am working on new project with combines voice recognition and GUI in which the users will interact with GUI by using their voice. and i have 3 main windows,.
- Main window => which is actual GUI for user. shown until users minimize it.
- Mini window => which stays at top to show user what he said. always shown.
- Text input window => which is user for user to enter and edit long paragraphs and text inputs using voice. shown when user needs to insert data and then back to hidden.
also i have a QThread which will give a text input form speech recognition to MainThread. so the problem is when i try to show the text input window and put convert text and hide it cause this error.
QObject::setParent: Cannot set parent, new parent is in a different thread
Segmentation fault -
@Bisrat said in QObject::setParent: Cannot set parent, new parent is in a different thread Segmentation fault:
can you edit the sample code i lost in here.
Remove
Qt.DirectConnection
EDIT: Ah I just realized you meant edit your forum post
-
Hi, and welcome!
@Bisrat said in QObject::setParent: Cannot set parent, new parent is in a different thread Segmentation fault:
also i have a QThread which will give a text input form speech recognition to MainThread. so the problem is when i try to show the text input window and put convert text and hide it cause this error.
You must only call GUI class methods from the GUI (main) thread. You are not allowed to call the methods from the other thread.
Instead, emit a signal to pass the text from your other thread to the GUI thread and then call the methods there.
-
I understand that but as far as i see i didn't call gui methods from other thread there is sample of my code where error raised
# IMPORT / GUI AND MODULES AND WIDGETS input_module = InputModule() class MainWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) # SET AS GLOBAL WIDGETS global input_module self.mini_window = MiniWindow() self.text_window = TextInputWindows() self.text_window.hide() self.worker_thread = WorkerThread(_input_module=input_module) self.worker_thread.start() self.connect(self.worker_thread, SIGNAL("thread_done(QString)"), self.thread_done, Qt.DirectConnection) # SHOW APP self.show() def thread_done(self, cmd): self.mini_window.set_text(cmd) if cmd == "maximize": self.maximize() elif cmd == "minimize" or cmd == "close": self.hide() elif cmd == "open window": self.show() elif cmd == "go to gmail": self.click_email_btn() self.click_youtube_btn() elif self.current_module == "News": if cmd == "input": print(threading.currentThread()) final_text = self.text_input() if final_text is not None: self.ui.NewsSearchText.setText(final_text) if cmd == "search": self.ui.NewsSearchBtn.click() def text_input(self): print(threading.currentThread()) self.text_window.show() while True: _cmd = input_module.take_final_input().lower() if _cmd == "add": self.text_window.set_whole_word_text(self.text_window.get_word_text()) elif _cmd == "clear": self.text_window.clear() elif _cmd == "go back": final_text = self.text_window.get_whole_word_text() self.text_window.clear() self.text_window.hide() return final_text else: self.text_window.set_word_text(_cmd) class MiniWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.ui = Ui_MiniVAHDP() self.ui.setupUi(self) self.setWindowFlags( Qt.Tool | Qt.FramelessWindowHint | Qt.Window | Qt.CustomizeWindowHint | Qt.WindowStaysOnTopHint) self.move(0, 0) self.show() def set_text(self, text): self.ui.MiniLabel.setText(text) def set_module(self, text): self.ui.ModuleLabel.setText(text) class TextInputWindows(QMainWindow): socketSignal = QtCore.Signal(object) def __init__(self): QMainWindow.__init__(self) self.ui = Ui_TextInput() self.ui.setupUi(self) self.setWindowFlags(Qt.FramelessWindowHint | Qt.CustomizeWindowHint | Qt.WindowStaysOnTopHint) self.show() def set_word_text(self, text): self.ui.WordText.setText(text) def set_whole_word_text(self, text): self.ui.WholeWordText.appendPlainText(text) def clear(self): self.ui.WholeWordText.setPlainText("") self.ui.WordText.setText("") def get_word_text(self): return self.ui.WordText.text() def get_whole_word_text(self): return self.ui.WholeWordText.toPlainText() class WorkerThread(QThread): def __init__(self, parent=None, _input_module=None): super(WorkerThread, self).__init__(parent) self.input_module = _input_module self.cmd = None self.sleep = False def run(self): while True: self.cmd = self.input_module.take_final_input().lower() if self.cmd == "wake up": self.sleep = False elif self.cmd == "sleep": self.sleep = True elif not self.sleep: self.emit(SIGNAL("thread_done(QString)"), self.cmd) if __name__ == "__main__": app = QApplication(sys.argv) app.setWindowIcon(QIcon("icon.ico")) window = MainWindow() print(threading.currentThread()) app.exec() # error raised on final_text = self.text_input()
-
@Bisrat said in QObject::setParent: Cannot set parent, new parent is in a different thread Segmentation fault:
I understand that but as far as i see i didn't call gui methods from other thread there is sample of my code where error raised
The formatting of the code went awry, and unfortunately formatting in python is everything. That said, this looks like a problem:
class MainWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) [...] self.connect(self.worker_thread, SIGNAL("thread_done(QString)"), self.thread_done, Qt.DirectConnection)
The direct connection means that the slot will be run in the thread that emits the signal. The slot appears to be defined in a QWidget derived class, and perform gui operations, and the signal appears to be emitted from a non-gui thread.
-
@Bisrat said in QObject::setParent: Cannot set parent, new parent is in a different thread Segmentation fault:
can you edit the sample code i lost in here.
Remove
Qt.DirectConnection
EDIT: Ah I just realized you meant edit your forum post