0

I made three QVboxlayout in main layout, and second QVboxlayout has sub three QHBoxlayout which has checkbox and groupbox.
Second QVboxlayout has layout named checkboxlayout.
Initial state

When Make button is clicked, 20 x 3 checkboxes and groupbox will be shown in second QVBoxlayout (=checkboxlayout).
When MAKE button clicked

and, Second Make button is clicked, I want clear boxes and labels but PyQT overwrites on just before layout
Duplicated index (See that index number appears duplicated : 1 and 2)

Without self.checkboxlayout.takeAt(0) code, then PyQT makes identical checkbox bottom
Without takeAt code

How can I remove first checkboxlayout so I can get clear second checkboxlayout?

def initUI(self):
    self.clickedidx = 0

    self.lineEdit1 = QLineEdit()
    self.pushButton1 = QPushButton(" Make ")
    self.pushButton2 = QPushButton(" I am dumb ")
    self.pushButton1.clicked.connect(self.onEnter)
    self.hlayout = QHBoxLayout()
    self.vlayout = QVBoxLayout()

    self.hlayout.addWidget(QLabel('Link:'), alignment=Qt.AlignTop)
    self.hlayout.addWidget(self.lineEdit1, alignment=Qt.AlignTop)
    self.hlayout.addWidget(self.pushButton1, alignment=Qt.AlignTop)
    self.vlayout.addLayout(self.hlayout)

    self.checkboxlayout = QVBoxLayout()
    self.vlayout.addLayout(self.checkboxlayout)

    self.hlayout = QHBoxLayout()
    self.hlayout.addWidget(self.pushButton2, alignment=Qt.AlignBottom)
    self.vlayout.addLayout(self.hlayout)

    self.setLayout(self.vlayout)
    self.center()
    self.show()

def onEnter(self):
    self.clickedidx += 1
        
    groupbox = QGroupBox()
    checkbox = []
    hlayout = QHBoxLayout()
    vlayout = QVBoxLayout()
    
    for i in range(1, 21):
        widget = QCheckBox( num2words(i) + " " + str(self.clickedidx) ) 
        vlayout.addWidget(widget)
    groupbox.setLayout(vlayout)
    hlayout.addWidget(groupbox)
        
    groupbox = QGroupBox()
    vlayout = QVBoxLayout()
    for i in range(21, 41):
        widget = QCheckBox( num2words(i) + " " + str(self.clickedidx) ) 
        vlayout.addWidget(widget)
    groupbox.setLayout(vlayout)
    hlayout.addWidget(groupbox)
    
    groupbox = QGroupBox()
    vlayout = QVBoxLayout()
    for i in range(41, 61):
        widget = QCheckBox( num2words(i) + " " + str(self.clickedidx) ) 
        vlayout.addWidget(widget)
    groupbox.setLayout(vlayout)
    hlayout.addWidget(groupbox)
    
    self.checkboxlayout.takeAt(0)
    self.checkboxlayout.addLayout(hlayout)

kat opp
  • 11
  • 2
  • 1
    please create an [example], and you shouldn't edit uic files – Alexander Aug 11 '22 at 04:42
  • If the number of checkboxes is always the same, what is the point of always deleting and recreating them? Why don't you just update their text and reset their status? – musicamante Aug 11 '22 at 11:03
  • I will create checkboxes dynamically at the end, and this is just test program. :) I couldn't think further except that deleting all of widgets and layouts, and creating new widgets and layouts. I am testing with changing the number of checkboxes, as you say, and it works well. Thank you for attention :-) – kat opp Aug 12 '22 at 02:07

1 Answers1

0

SOLVED.

I modified/added below code end of onEnter() method

item = self.checkboxlayout.takeAt(0)
self.checkboxlayout.addLayout(hlayout)
    if self.clickedidx > 1 :
        widget = item.widget()
        if widget is not None:
            widget.setParent(None)
        else:
            deleteItemsOfLayout( item.layout() )

and added function deleteItemsOfLayout(layout)

def deleteItemsOfLayout(layout):
    if layout is not None:
        while layout.count():
            item = layout.takeAt(0)
            widget = item.widget()
            if widget is not None:
                widget.setParent(None)
            else:
                deleteItemsOfLayout(item.layout())

The problem was that self.checkboxlayout.takeAt(0) delete old checkboxlayout,
but DOES NOT DELETE old widgets, so result shows duplicated widgets.

Helped from this :
PyQt How to remove a layout from a layout

kat opp
  • 11
  • 2