Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Removed widget item in QVBoxLayout still visible, why ?



  • I have the following code. I connect out the Clear menu button to an action which goes about removing the items I added.

    There was runtime errors that I can see but the widget inside the QVBoxLayout still hang around. Is it because I am deleting them wrongly or I need to call some refresh method explicitly to trigger a redraw of the entire layout ?

    main.py

    import sys
    from PySide2 import QtWidgets, QtGui
    
    from MainWindow import Ui_MainWindow
    
    
    class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setupUi(self)
    
            # Further location UI customization
            self.actionQuit.setShortcut(QtGui.QKeySequence.Quit)
            self.actionQuit.triggered.connect(self.close)
    
            self.actionAdd.triggered.connect(self.Add)
            self.actionClear.triggered.connect(self.Clear)
    
            vlayout = self.findChild(QtWidgets.QVBoxLayout, "verticalLayout")
            # vlayout.addWidget(QtWidgets.QLabel("Parameters"))
    
        def Add(self):
            print('Adding')
            self.verticalLayout.addWidget(QtWidgets.QLabel("Adding"))
    
        def Remove(self):
            print('Remove')
            child = self.verticalLayout.takeAt(0)
            if child:
                self.verticalLayout.removeItem(child)
                del child
                self.verticalLayout.update()
    
        def Clear(self):
            print('Clear number of widgets = {}'.format(self.verticalLayout.count()))
            child = self.verticalLayout.takeAt(0)
            while child:
                self.verticalLayout.removeItem(child)
                del child
                child = self.verticalLayout.takeAt(0)
            self.verticalLayout.update()
    
    app = QtWidgets.QApplication(sys.argv)
    
    window = MainWindow()
    window.show()
    app.exec_()
    

    MainWindow.py

    # -*- coding: utf-8 -*-
    
    ################################################################################
    ## Form generated from reading UI file 'MainWindow.ui'
    ##
    ## Created by: Qt User Interface Compiler version 5.15.2
    ##
    ## WARNING! All changes made in this file will be lost when recompiling UI file!
    ################################################################################
    
    from PySide2.QtCore import *
    from PySide2.QtGui import *
    from PySide2.QtWidgets import *
    
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            if not MainWindow.objectName():
                MainWindow.setObjectName(u"MainWindow")
            MainWindow.resize(800, 600)
            self.actionAdd = QAction(MainWindow)
            self.actionAdd.setObjectName(u"actionAdd")
            self.actionClear = QAction(MainWindow)
            self.actionClear.setObjectName(u"actionClear")
            self.actionQuit = QAction(MainWindow)
            self.actionQuit.setObjectName(u"actionQuit")
            self.centralwidget = QWidget(MainWindow)
            self.centralwidget.setObjectName(u"centralwidget")
            self.gridLayout = QGridLayout(self.centralwidget)
            self.gridLayout.setObjectName(u"gridLayout")
            self.verticalLayout = QVBoxLayout()
            self.verticalLayout.setObjectName(u"verticalLayout")
    
            self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
    
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QMenuBar(MainWindow)
            self.menubar.setObjectName(u"menubar")
            self.menubar.setGeometry(QRect(0, 0, 800, 22))
            self.menuFile = QMenu(self.menubar)
            self.menuFile.setObjectName(u"menuFile")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QStatusBar(MainWindow)
            self.statusbar.setObjectName(u"statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.menubar.addAction(self.menuFile.menuAction())
            self.menuFile.addAction(self.actionAdd)
            self.menuFile.addAction(self.actionClear)
            self.menuFile.addSeparator()
            self.menuFile.addAction(self.actionQuit)
    
            self.retranslateUi(MainWindow)
    
            QMetaObject.connectSlotsByName(MainWindow)
        # setupUi
    
        def retranslateUi(self, MainWindow):
            MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
            self.actionAdd.setText(QCoreApplication.translate("MainWindow", u"Add", None))
            self.actionClear.setText(QCoreApplication.translate("MainWindow", u"Clear", None))
            self.actionQuit.setText(QCoreApplication.translate("MainWindow", u"Quit", None))
            self.menuFile.setTitle(QCoreApplication.translate("MainWindow", u"File", None))
        # retranslateUi
    

  • Lifetime Qt Champion

    @nicholas_yue said in Removed widget item in QVBoxLayout still visible, why ?:

    There was runtime errors that I can see

    Please post those errors.
    Did you make sure that the slot was called?



  • Try set the removed item parent to None.


Log in to reply