i am trying to make a UI that display folders from a given directory, and the user can check or uncheck the folder. i would like to get the information of what was checked and return the folder i know i can loop / iterate in a QTableWidget, but if its a view, or its comming from a Qt.Dir how its done?

(even might be a easier way to add the check boxes to the dir view model than making another class)

thanks.

from PyQt4 import QtCore, QtGui import sys import argparse

def parseOpt():
    parser = argparse.ArgumentParser(description="Check if the files in this folder are valid EXRs")
    parser.add_argument("-file", dest="filepath", help="The file path to be checked.")
    return parser.parse_args()
ARGS = parseOpt()

class CheckableDirModel(QtGui.QDirModel):
    #a class to put checkbox on the folders
    def __init__(self, parent=None):
        QtGui.QDirModel.__init__(self, None)
        self.checks = {}

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

    def flags(self, index):
        return QtGui.QDirModel.flags(self, index) | QtCore.Qt.ItemIsUserCheckable

    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):
            self.checks[index] = value
            self.emit(QtCore.SIGNAL("dataChanged(QModelIndex,QModelIndex)"), index, index)
            return True 

        return QtGui.QDirModel.setData(self, index, value, role)

    #def filtering(self, index):
    #   self.checks.setFilter(QtCore.QDir.Dirs|QtCore.QDir.NoDotAndDotDot)




class Ui_Dialog(QtGui.QDialog):
    def __init__(self,parent=None):
        QtGui.QDialog.__init__(self,parent)
        self.setObjectName("Dialog")
        self.resize(600, 500)

        self.llayout = QtGui.QVBoxLayout(parent)

        self.model = CheckableDirModel()
        self.model.setFilter(QtCore.QDir.Dirs|QtCore.QDir.NoDotAndDotDot)

        #self.tree = QtGui.QTreeWidget()
        self.tree = QtGui.QTreeView()
        self.tree.setModel(self.model)
        self.tree.setSortingEnabled(True)
        self.tree.setRootIndex(self.model.index(ARGS.filepath))

        #self.tree.hideColumn(1)
        #self.tree.hideColumn(2)
        #self.tree.hideColumn(3)

        self.tree.setWindowTitle("Dir View")
        self.tree.resize(400, 480)
        self.tree.setColumnWidth(0,200)

        self.but = QtGui.QPushButton(QtCore.QString("Run"))


        self.llayout.addWidget(self.tree)
        self.llayout.addWidget(self.but)

        self.setLayout(self.llayout)

        self.but.clicked.connect(self.print_path)

    def print_path(self):
        print "hello"
        root = self.tree.childCount()
        print root
        for i in range(root):
            print i.text()

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    ui = Ui_Dialog()
    ui.show()
    sys.exit(app.exec_())
有帮助吗?

解决方案

If I understand you correctly, what you want is to replace your print_path method with the following:

    def print_path(self):
        print "hello"
        for index,value in self.model.checks.items():
            if value.toBool():
                print self.model.filePath(index)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top