Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QObject::setParent: Cannot set parent, new parent is in a different thread Segmentation fault

QObject::setParent: Cannot set parent, new parent is in a different thread Segmentation fault

Scheduled Pinned Locked Moved Solved Qt for Python
8 Posts 3 Posters 4.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bisrat
    wrote on last edited by
    #1

    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

    JKSHJ 1 Reply Last reply
    0
    • B Bisrat

      @jeremy_k Thanks for response.
      can you edit the sample code i lost in here.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #6

      @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

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • B Bisrat

        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

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by JKSH
        #2

        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.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        3
        • B Offline
          B Offline
          Bisrat
          wrote on last edited by JKSH
          #3

          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()
          
          jeremy_kJ 1 Reply Last reply
          0
          • B Bisrat

            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()
            
            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #4

            @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.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            B 1 Reply Last reply
            3
            • jeremy_kJ jeremy_k

              @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.

              B Offline
              B Offline
              Bisrat
              wrote on last edited by
              #5

              @jeremy_k Thanks for response.
              can you edit the sample code i lost in here.

              JKSHJ jeremy_kJ 2 Replies Last reply
              0
              • B Bisrat

                @jeremy_k Thanks for response.
                can you edit the sample code i lost in here.

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by JKSH
                #6

                @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

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • B Bisrat

                  @jeremy_k Thanks for response.
                  can you edit the sample code i lost in here.

                  jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on last edited by
                  #7

                  @Bisrat I don't have the ability to edit other users' posts. You can by clicking the column of three dots to the right of the bottom of your post, and then choosing edit.

                  11e01094-09e3-450c-b98e-e65a561a442d-image.png

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Bisrat
                    wrote on last edited by
                    #8

                    Thanks it worked fine

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved