如何使QTreeView CheckBox预先选中

时间:2017-11-29 06:53:45

标签: python pyqt pyqt4 qtreeview qfilesystemmodel

我有一个带有QtGui.QFileSystemModel的TreeView来呈现文件系统文件夹。

我的TreeView有一个复选框,并检查&取消工作正常。我想预先选择UI Load上的所有复选框。

请建议如何为TreeView设置默认选中复选框。

我的模型代码是: -

class CheckableDirModel(QtGui.QFileSystemModel):

    def __init__(self, resultFormats, parent=None):
        QtGui.QFileSystemModel.__init__(self, None)
        filters = QStringList()
        #import ipdb;ipdb.set_trace()
        filters.append('*.odb')
        resultFormats = resultFormats.split(',')
        for resultFormat in resultFormats:
            filters.append('*.' + resultFormat)
        self.setNameFilters(filters)
        #set to False to hide files that match the filter
        self.setNameFilterDisables(True)
        self.checks = {}
        self.selectedFiles = set()

        #self.b1 = QCheckBox("SPM_CAE_DATA")
        #self.b1.setChecked(True)


    #Show the particular formate ('.pages') file into result set for the checked folder
    def addFile(self,path1):
        # import ipdb;ipd b.set_trace()
        files = glob.iglob(os.path.join(str(path1), "*.pages"))#size= os.path.getsize(fp)
        for file in files:
            print(file)
            if os.path.isfile(file):
                #print(file)
                Formate.append(file)
                shutil.copy2(file, reportpath + '/../Particular_Formate')


        #print(Formate)
        #treeView1 = TreeView(reportpath)
    #Show the file greater than 250 MB into list 


    def addFilegt250mb(self,path1):
        # import ipdb;ipd b.set_trace()
        global listview1
        for root, dirs, files in os.walk(str(path1), topdown=False):
            for name in files:
                f = os.path.join(root, name)
                if os.path.isfile(f):#if os.path.isfile(f)
                    if os.path.getsize(f) >= 0:
                        greaterSize.append(f)
                        print(name)
                        shutil.copy2(f, reportpath+ '/../Particular_Formate_size')


        #print(greaterSize)


    #remove the File, When uncheck the Folder
    def deleteFile(self,path2):
        for root, dirs, files in os.walk(str(path2), topdown=False):
            for name in files:
                f = os.path.join(root, name)
                if name == ".DS_Store" :
                    pass

                else:
                    if os.path.getsize(f) >=0 :
                        greaterSize.remove(f)

                for x in Formate:
                    if x == f :
                        Formate.remove(x)


                    #Formate.remove(f)


                self.deletSize(name)
                self.deletFormate(name)

        #print(greaterSize)
        #print(Formate)        
    #remove the file size greater then 250 MB , When Folder is uncheck
    def deletSize(self,name)  :
        folder = reportpath +'/../Particular_Formate_size'
        for the_file in os.listdir(folder):
            file_path = os.path.join(folder, the_file)
            if the_file == name :
                try:
                    if os.path.isfile(file_path):
                        os.unlink(file_path)

                    #elif os.path.isdir(file_path): shutil.rmtree(file_path)
                except Exception as e:
                    print(e)
        #print(greaterSize)
    #Renove the particular formate file
    def deletFormate(self,name)  :
        folder = reportpath + '/../Particular_Formate'
        for the_file in os.listdir(folder):
            file_path = os.path.join(folder, the_file)
            if the_file == name :
                try:
                    if os.path.isfile(file_path):
                        os.unlink(file_path)
                        #import ipdb;ipdb.set_trace()
                    #elif os.path.isdir(file_path): shutil.rmtree(file_path)
                except Exception as e:
                    print(e)



    def data(self, index, role=QtCore.Qt.DisplayRole):
        if role != QtCore.Qt.CheckStateRole:
            return QtGui.QFileSystemModel.data(self, index, role)
        else:
            if index.column() == 0:
                return self.checkState(index)



    def flags(self, index):
        if (index.column() == 0):
            return QtGui.QFileSystemModel.flags(self, index) | QtCore.Qt.ItemIsUserCheckable
        else:
            return QtGui.QFileSystemModel.flags(self, index)



    def checkState(self, index):
        if index in self.checks:
            return self.checks[index]
        else:
            return QtCore.Qt.Unchecked

    def setData(self, index, value, role):
        if (role == QtCore.Qt.CheckStateRole and index.column() == 0):
           # print(QtGui.QFileSystemModel.filePath(self, index))

            self.checks[index] = value
            if QtGui.QFileSystemModel.filePath(self, index) in self.selectedFiles:
                self.selectedFiles.remove(QtGui.QFileSystemModel.filePath(self, index))
                path2=QtGui.QFileSystemModel.filePath(self, index)
                self.deleteFile(path2)


            else:
                path1 =QtGui.QFileSystemModel.filePath(self, index)
                self.addFile(path1)
                self.addFilegt250mb(path1)

                self.selectedFiles.add(QtGui.QFileSystemModel.filePath(self, index))

            self.emit(QtCore.SIGNAL("dataChanged(QModelIndex,QModelIndex)"), index, index)
            return True 
        return QtGui.QFileSystemModel.setData(self, index, value, role)

Source Code

我尝试使用不同的组合进行Checkbox预选,但无法成功。

0 个答案:

没有答案
相关问题