【问题标题】:PyQt iterate over a a QTreeView with checkboxPyQt 用复选框遍历一个 QTreeView
【发布时间】:2013-11-07 19:17:38
【问题描述】:

我正在尝试制作一个显示给定目录中的文件夹的 UI, 并且用户可以选中或取消选中该文件夹。 我想获取检查内容的信息并返回文件夹 我知道我可以在 QTableWidget 中循环/迭代,但如果它是一个视图,或者它来自 Qt.Dir,它是如何完成的?

(甚至可能比创建另一个类更容易将复选框添加到目录视图模型)

谢谢。

从 PyQt4 导入 QtCore、QtGui 导入系统 导入参数解析

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_())

【问题讨论】:

    标签: checkbox pyqt qdir


    【解决方案1】:

    如果我对您的理解正确,您想要的是将您的 print_path 方法替换为以下内容:

        def print_path(self):
            print "hello"
            for index,value in self.model.checks.items():
                if value.toBool():
                    print self.model.filePath(index)
    

    【讨论】:

    • @user1704282 如果我的回复解决了您的问题,请“接受我的回复”(我的回复左侧应该有一个绿色勾号才能点击)。干杯
    猜你喜欢
    • 2011-12-31
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    相关资源
    最近更新 更多