PyQt Tree Widget, adding check boxes for dynamic removal

前端 未结 4 743
一生所求
一生所求 2020-12-28 11:21

I am attempting to create a tree widget that will essentially allow the user to view various breakdowns of data and have the option to delete certain items. In order to do t

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-28 11:48

    I ported @andy's awesome example to PyQt5:

    from PyQt5 import QtWidgets
    from PyQt5 import QtCore
    from PyQt5 import QtGui
    from PyQt5.Qt import Qt
    import sys
    
    def main(): 
        app     = QtWidgets.QApplication(sys.argv)
        tree    = QtWidgets.QTreeWidget()
        headerItem  = QtWidgets.QTreeWidgetItem()
        item    = QtWidgets.QTreeWidgetItem()
    
        for i in range(3):
            parent = QtWidgets.QTreeWidgetItem(tree)
            parent.setText(0, "Parent {}".format(i))
            parent.setFlags(parent.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable)
            for x in range(5):
                child = QtWidgets.QTreeWidgetItem(parent)
                child.setFlags(child.flags() | Qt.ItemIsUserCheckable)
                child.setText(0, "Child {}".format(x))
                child.setCheckState(0, Qt.Unchecked)
        tree.show() 
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    

    screenshot

提交回复
热议问题