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