Pyside6: prevent QGroupBox getting smaller when added to a QScrollArea

277 views Asked by At

I am trying to add some group-boxes containing some other widgets to a scroll area (which has a vertical layout in a QWidget). But when I click the button several times to add new group-boxes, they just get smaller and smaller.

Adding one rule:

Adding one rule

Adding three rules:

Adding three rules

Adding a lot of rules:

Adding a lot of Rules

Here is the code I am using to add the group-boxes to the scroll area:

from PySide6.QtCore import *
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6 import QtWidgets
from views.Ui_form import Ui_MainWindow

class MainWindowForm(QWidget, Ui_MainWindow):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.anadir_regla.clicked.connect(self.add)
        self.eliminar_regla.clicked.connect(self.delete)
        n = 1
        if self.anadir_regla.clicked == True:
            n = n + 1
        #top_widget = QtWidgets.QWidget()
    #def sum_c(num):
    #    return sum(int(c) for c in num if c.isdecimal())

    
    def add(self, n): #funcion para aƱadir
        Regla = QGroupBox()
        Regla.setObjectName(u"Regla")
        Regla.setTitle(QCoreApplication.translate("MainWindow", u"Regla "+str(n), None))
        Regla.setGeometry(QRect(0, 10, 791, 80))
        
        layout = QHBoxLayout(Regla)

        #Frame interno de las reglas

        combo_objeto = QComboBox(self.Regla)
        combo_objeto.setObjectName(u"combo_objeto")
        combo_objeto.setGeometry(QRect(10, 20, 151, 22))
        combo_objeto.setMaxVisibleItems(2)
        combo_objeto.setPlaceholderText(QCoreApplication.translate("MainWindow", u"Elije un Objeto", None))
        layout.addWidget(combo_objeto)

        id_objeto = QLineEdit(self.Regla)
        id_objeto.setObjectName(u"id_objeto")
        id_objeto.setGeometry(QRect(190, 20, 161, 22))
        layout.addWidget(id_objeto)

        combo_accion = QComboBox(self.Regla)
        combo_accion.setObjectName(u"combo_accion")
        combo_accion.setGeometry(QRect(380, 20, 141, 22))
        layout.addWidget(combo_accion)

        combo_resultado = QComboBox(self.Regla)
        combo_resultado.setObjectName(u"combo_resultado")
        combo_resultado.setGeometry(QRect(540, 20, 141, 22))
        layout.addWidget(combo_resultado)
        
        self.lay_reglas.addWidget(Regla)
        self.top_widget = (self.lay_reglas)
        self.scrollArea.setWidget(self.top_widget)
     
    def delete(self): #eliminar Reglas
        count = self.lay_reglas.count()
        item = self.lay_reglas.itemAt(count - 1)
        widget = item.widget()
        widget.deleteLater()

PS: The adding and deleting buttons work fine.

0

There are 0 answers