2

这是我之前的问题的延续。。“如何从左上角开始将小部件添加到网格布局”感谢 X.Jacobs修复 tat 问题。现在我想删除所有这些小部件并重新创建相同的小部件相同的顺序,比如刷新小部件库.. 谢谢

在此处输入图像描述

好的,这是我的 ui.. 当我单击 createPose 按钮​​时,它将调用 3 Def..

1 . 在特定文件夹中创建一个txt文件

2 . 清除网格布局中的所有小部件...

3 . 最后根据特定文件夹中的文件数量和文件名重新创建我的所有小部件,例如添加小部件并刷新库..但与以前的顺序相同..

        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

现在我添加了一个功能来按创建日期对我的文件夹文件进行排序..现在我的网格布局以正确的顺序排列我的按钮..但是按钮的位置正在跳跃..就像它们之间的间隙..我已经上传了我的屏幕也拍了

4

1 回答 1

4

从布局中删除所有小部件如下所示:

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
    # i.e.:   aList.append(widget.someId)
    widget.deleteLater()
于 2012-12-22T19:29:34.823 回答