How to remove all widgets inside a gridlayout widget and recreate all widgets in the same order

this is a continuation of my previous question here. "How to add a widget to the gridlayout, starting from the upper left corner," thanks X.Jacobs to fix the tat question .. Now I want to remove all these widgets and recreate the same widgets in the same order as updating the widget library. thanks

enter image description here

ok this is my ui .. when i click the createPose button it calls 3 Def ..

1 . create a txt file in a specific folder

2 . clear all my widgets in my grid layout ...

3 . and finally recreate all my widgets based on the number of files and file names in a specific folder, for example, add a widget and update the library ... but in the same order as before, when it was ..

self.connect(self.ui.CreatePose_pushButton, QtCore.SIGNAL("clicked()"), self.createPose) def createPose(self): # To get Posename from UI self.__current_Posename = self.getPoseName() Path = 'D:\\PoseLibrary\\' # To Grab All files in tat Path Poses = self.findAllFiles(Path, '.xml') self.__NameSpace = self.ui.NameSpace_comboBox.currentText() #To find HighestTrailingNumber to Increment the file name if self.__current_Posename == "": newSuffix = self.findHighestTrailingNumber(Poses, self.__Default_Posename) + 1 self.PoseName = self.__Default_Posename + str(newSuffix) else: self.PoseName = self.__current_Posename # Creating XML and Icon files open(Path + self.PoseName + '.xml','w').close() icon = self.createIcon(self.PoseName, Path) # open(Path + self.PoseName + '.png','w').close() self.refreshPoseLibrary(Path) self.ui.PoseName_lineEdit.clear() def refreshPoseLibrary(self, Path ): # First Clear all my buttons in gridlayout self.deleteAll() # get all file fromm the path dir Files = self.getfiles_by_Date(Path) Poses = self.findAllFiles(Files, '.xml') # Create Btn for Pose in Poses: icon = Path + Pose + '.png' self.icon.addPixmap(QtGui.QPixmap(icon), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.button = QtGui.QPushButton('') self.button.setObjectName(Pose) self.buttonGroup.addButton(self.button) self.button.setIcon(self.icon) self.button.setIconSize(QtCore.QSize(128, 128)) self.button.setMinimumSize(QtCore.QSize(128, 128)) self.button.setMaximumSize(QtCore.QSize(128, 128)) self.ui.gridLayout.addWidget(self.button) self.button.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.connect(self.button, QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'), self.popup) def deleteAll(self): while self.ui.gridLayout.count(): item = self.ui.gridLayout.takeAt(0) widget = item.widget() widget.deleteLater() def getfiles_by_Date(self, dirpath): Files = [s for s in os.listdir(dirpath) if os.path.isfile(os.path.join(dirpath, s))] Files.sort(key=lambda s: os.path.getmtime(os.path.join(dirpath, s))) return Files 

Now I added another function to sort the folder files by creation date .. now my gridlayout arranges my buttons in the correct order .. but the position of the button jumps .. like the gap between them .. I uploaded my screenshot

+1
source share
1 answer

Removing all widgets from the layout is as follows:

 while layout.count(): item = layout.takeAt(0) widget = item.widget() # if widget has some id attributes you need to # save in a list to maintain order, you can do that here # ie: aList.append(widget.someId) widget.deleteLater() 
+2
source

All Articles