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. Best practice: QDialog accesing QMainWindow attribute

Best practice: QDialog accesing QMainWindow attribute

Scheduled Pinned Locked Moved Solved Qt for Python
8 Posts 3 Posters 1.1k Views 1 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.
  • I Offline
    I Offline
    Izabel
    wrote on last edited by
    #1

    Hi!

    I'm using PyQt for a project and I have some doubts of how to make things the proper way.

    Let's say for example that I have a list of names that may change as an attribute of MainWindow, and I want to display those names in a QComboBox that is in a QDialog in another class. What would be the best way to connect them?

    Thanks.

    JonBJ 1 Reply Last reply
    0
    • I Izabel

      Hi!

      I'm using PyQt for a project and I have some doubts of how to make things the proper way.

      Let's say for example that I have a list of names that may change as an attribute of MainWindow, and I want to display those names in a QComboBox that is in a QDialog in another class. What would be the best way to connect them?

      Thanks.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Izabel
      Hello and welcome.

      It's simple: just do not do what most beginners seems to try, which is to have the dialog know anything about or be able to call anything in the MainWindow! That is the wrong way to go. Other windows/dialogs/classes should know nothing about your main window, do not even allow yourself to import the MainWindow module inot any other module so you won't be tempted.

      Assuming you mean only MainWindow knows the list of names to populate the combobox, write yourself a utility "setter" method in the dialog to receive the list to be used and it populates the combobox. And maybe a "getter" method to return which one selected if wanted by the outside world. Something like:

      # MainWindow
      import MyDialog
      
      myDialog = MyDialog(self)
      myDialog.setComboItems(["abc", "def", ...])
      if myDialog.exec():
          chosenItem = myDialog.comboItem()
      
      # MyDialog
      def setComboItems(self, items):
          self.combo.addItems(items)
      
      def comboItem(self):
          return self.combo.currentText()
      
      I 1 Reply Last reply
      3
      • I Offline
        I Offline
        Izabel
        wrote on last edited by
        #3

        Thank you so much! As a beginner that was what I was trying to do at first but I knew it was a bad idea haha.

        Nice, I got it! Happy Holidays!

        1 Reply Last reply
        1
        • JonBJ JonB

          @Izabel
          Hello and welcome.

          It's simple: just do not do what most beginners seems to try, which is to have the dialog know anything about or be able to call anything in the MainWindow! That is the wrong way to go. Other windows/dialogs/classes should know nothing about your main window, do not even allow yourself to import the MainWindow module inot any other module so you won't be tempted.

          Assuming you mean only MainWindow knows the list of names to populate the combobox, write yourself a utility "setter" method in the dialog to receive the list to be used and it populates the combobox. And maybe a "getter" method to return which one selected if wanted by the outside world. Something like:

          # MainWindow
          import MyDialog
          
          myDialog = MyDialog(self)
          myDialog.setComboItems(["abc", "def", ...])
          if myDialog.exec():
              chosenItem = myDialog.comboItem()
          
          # MyDialog
          def setComboItems(self, items):
              self.combo.addItems(items)
          
          def comboItem(self):
              return self.combo.currentText()
          
          I Offline
          I Offline
          Izabel
          wrote on last edited by
          #4

          @JonB
          Hi again!

          I want to be 1000% clear. I've seen a lot of examples of people accessing parent attributes whitin QDialog saving a parent reference or accessing them as self.parent().attribute_name.

          That would be an example of what I shouldn't do, right?

          Thanks again.

          M JonBJ 2 Replies Last reply
          0
          • I Izabel

            @JonB
            Hi again!

            I want to be 1000% clear. I've seen a lot of examples of people accessing parent attributes whitin QDialog saving a parent reference or accessing them as self.parent().attribute_name.

            That would be an example of what I shouldn't do, right?

            Thanks again.

            M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #5

            @Izabel said in Best practice: QDialog accesing QMainWindow attribute:

            That would be an example of what I shouldn't do, right?

            Nothing is 100% bad or good.
            You need to understand what you are doing.
            When you pass a reference of your MainWidow to the Dialog, you create a strong dependency between the two objects (tight coupling)
            Work well in your case, now suppose you need the same utility dialog somewhere else in your program where you have no access to the Maindow , you're doomed :)

            By experience, programmers know that creating tight coupling is bad and can lead to nightmare issues.
            And the main avantage of object programming is to produce reusable objects, so you must have this in mind and eliminate all dependencies as much as possible.

            I 1 Reply Last reply
            2
            • I Izabel

              @JonB
              Hi again!

              I want to be 1000% clear. I've seen a lot of examples of people accessing parent attributes whitin QDialog saving a parent reference or accessing them as self.parent().attribute_name.

              That would be an example of what I shouldn't do, right?

              Thanks again.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Izabel
              Think of real life. You're a parent and you want some information from one of your children to store in your bedroom. Would you rather ask the child for the information and put it there yourself, or have some child rummaging around in your bedroom and hoping s/he doesn't destroy it? ;-)

              For the rest as @mpergand said.

              I would suggest you would find your life easier if you try not to call into parent from children. Qt can also make this easier with its signals/slots mechanism.

              I 1 Reply Last reply
              2
              • M mpergand

                @Izabel said in Best practice: QDialog accesing QMainWindow attribute:

                That would be an example of what I shouldn't do, right?

                Nothing is 100% bad or good.
                You need to understand what you are doing.
                When you pass a reference of your MainWidow to the Dialog, you create a strong dependency between the two objects (tight coupling)
                Work well in your case, now suppose you need the same utility dialog somewhere else in your program where you have no access to the Maindow , you're doomed :)

                By experience, programmers know that creating tight coupling is bad and can lead to nightmare issues.
                And the main avantage of object programming is to produce reusable objects, so you must have this in mind and eliminate all dependencies as much as possible.

                I Offline
                I Offline
                Izabel
                wrote on last edited by
                #7

                @mpergand Thank you so much! :)

                1 Reply Last reply
                0
                • JonBJ JonB

                  @Izabel
                  Think of real life. You're a parent and you want some information from one of your children to store in your bedroom. Would you rather ask the child for the information and put it there yourself, or have some child rummaging around in your bedroom and hoping s/he doesn't destroy it? ;-)

                  For the rest as @mpergand said.

                  I would suggest you would find your life easier if you try not to call into parent from children. Qt can also make this easier with its signals/slots mechanism.

                  I Offline
                  I Offline
                  Izabel
                  wrote on last edited by
                  #8

                  @JonB

                  Hahaha that's a really good example. Yeah, I'd definitely prefer to ask them for the information.

                  Thank you so much! :)

                  1 Reply Last reply
                  1

                  • Login

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