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. Is this safe? Do I have to use signals to call UI widget methods?
QtWS25 Last Chance

Is this safe? Do I have to use signals to call UI widget methods?

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 5 Posters 533 Views
  • 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.
  • I Offline
    I Offline
    ideaplus
    wrote on last edited by ideaplus
    #1

    Is this safe? Do I have to use signals to call UI widget methods on the main thread?

    import sys
    
    from PySide2.QtCore import QThread
    from PySide2.QtWidgets import QApplication, QWidget
    
    
    class MyThread(QThread):
        def __init__(self, main_window, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.main_window = main_window
    
        def run(self):
            # Is this safe? Do I have to use signals to call UI widget methods?
            print(self.main_window.geometry())
            # Is this safe? Do I have to use signals to call UI widget methods?
            self.main_window.hide()
            print(self.parent())
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        my_widget = QWidget()
        my_widget.show()
        my_thread = MyThread(my_widget)
        my_thread.start()
        sys.exit(app.exec_())
    

    refer link:
    https://stackoverflow.com/questions/53058849/is-it-ok-to-read-qt-widgets-from-another-thread
    https://stackoverflow.com/questions/42876085/is-it-possible-to-hide-qt-widget-window-from-other-thread

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      No its not safe.
      Say your thread reads main_window.geometry() at the exact same time
      you resize the window and the main thread updates the info.

      Its best to use signals and slots for this.

      Else its just a crash waiting to happen.

      1 Reply Last reply
      3
      • I Offline
        I Offline
        ideaplus
        wrote on last edited by
        #3

        Thanks. I understand. :-)

        1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Denni-0 A QWidget can be placed in a QThread, but it is not supported and can lead to crashes and misbehaviour. And nothing stops you from calling methods on widgets from a non GUI thread (but of course you should not). So, @mrjj is not wrong.

          1 Reply Last reply
          2
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Technically: QWidget based objects should only be manipulated in the same thread where the QApplication object was created. Usually, it's in the main thread, the one created to run the "main" function but it can be another one although it's not the most used technique.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Did you actually take the time to fully understand what I wrote ? At no point did I say that you could use a QWidget in a secondary thread beside the main thread, quite the contrary. I wrote that the thread creating the QApplication object was the only one where you are allowed to modify QWidget objects and that this precise thread might be a secondary thread even if it's not the common use case.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7
                This post is deleted!
                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