'Why won't my new layout be displayed dynamically on my GUI ? Python - PyQt
I'm trying to print some text in a new layout when a button is pressed. Unfortunately if won't work and I can't find the problem to solve it.
Thank you for your help !
Here's my code :
class GeometrieWidget(QWidget):
def __init__(self):
super(GeometrieWidget, self).__init__()
self.setGeometry(0, 0, 500, 400)
################ No problem here do not bother with this part############################
# Groupe filtres
self.group_filtres = QGroupBox("Filtres Contours")
self.filtrePerimetre = MyRangeSliderTool("Périmètre", 0, 10000, "ActivateDeactivate", False)
self.filtreAire = MyRangeSliderTool("Aire", 0, 1000000, "ActivateDeactivate", False)
self.filtreCircularite = MyDoubleRangeSliderTool("Circularité", 0, 1, "ActivateDeactivate", False)
self.filtreRatioLh = MyDoubleRangeSliderTool("Ratio h/L", 0, 1, "ActivateDeactivate", False)
self.group_filtres.elements = [self.filtrePerimetre, self.filtreAire,
self.filtreRatioLh, self.filtreCircularite]
layout = QVBoxLayout()
for element in self.group_filtres.elements:
layout.addWidget(element)
self.group_filtres.setLayout(layout)
nbElements = len(self.group_filtres.elements)
self.group_filtres.setFixedHeight(40 * (1 + nbElements))
# Groupe affichage
self.group_affichage = QGroupBox("Affichage")
self.contoursLine = MyToggle("Contours", True)
self.boundingBoxes = MyToggle("Bounding Boxes", False)
self.group_affichage.elements = [self.contoursLine, self.boundingBoxes]
layout = QVBoxLayout()
for element in self.group_affichage.elements:
layout.addWidget(element)
self.group_affichage.setLayout(layout)
nbElements = len(self.group_affichage.elements)
self.group_affichage.setFixedHeight(40 * (1 + nbElements))
################ Problem starts here I believe ############################
# button that calls drawImagette() when clicked on
self.button = QPushButton("Création d'imagettes", self)
self.button.move(5,20)
self.button.clicked.connect(self.drawImagettes)
# Layout Principal
self.layout = QVBoxLayout()
self.spaceLabel = QLabel()
self.spaceLabel.setFixedHeight(300)
self.layout.addWidget(self.group_filtres)
self.layout.addWidget(self.group_affichage)
self.layout.addWidget(self.button)
self.layout.setAlignment(Qt.AlignTop)
self.layout.addStretch(1)
self.setLayout(self.layout)
# function that's supposed to print "imagette 1", "imagette 2", "imagette 3" when the button is clicked on
def drawImagettes(self) :
self.group_imagettes = QGroupBox("Imagettes")
self.imagettes = MyListItem(["imagette 1", "imagette 2", "imagette 3"])
self.group_imagettes.elements = [self.imagettes]
self.layout = QVBoxLayout()
self.layout.addWidget(self.group_imagettes)
self.layout.addStretch(1)
The problem is it won't print the text in a layout even though I could confirm that the program goes into drawImagette when runnning thank to some 'print'. What is wrong with my code ?
Solution 1:[1]
I can't find an attribute called elements for QGroupBox or its parent (QWidget) in the doc so I think when you are doing self.group_imagettes.elements = [self.imagettes]
you are just adding you list of imagettes as an attribute of you QGroupbox but this attribute is never used (Qt doesn't know it exist since you added it, so it can't display it).
Also I don't know what kind of object MyListItem is so not sure how to read your code but you would also need to but your text in widgets that you add to your layout (like a QPlainTextEdit).
The doc also mentions "QGroupBox doesn’t automatically lay out the child widgets" and their example applied to your code (with also the QPlainTextEdit) would give something like this:
def drawImagettes(self) :
self.group_imagettes = QGroupBox("Imagettes")
self.imagettes = [QPlainTextEdit(txt) for txt in ["imagette 1", "imagette 2", "imagette 3"]]
layout = QVBoxLayout()
[layout.addWiget(imagette) for imagette in self.imagettes]
self.group_imagettes.setLayout(layout)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |