QTreeWidget的topLevelItemCount属性是一个只读属性,用于保存树型部件中顶层项的个数,可以通过topLevelItemCount()方法获取属性值,缺省值为0。
当树型部件中顶层项的数目变化时,topLevelItemCount自动跟随变化。
案例:
def initTopItems(self):
cdriver = QtWidgets.QTreeWidgetItem(1,2,["c:\\","NTFS","10G"])
ddriver = QtWidgets.QTreeWidgetItem(["d:\\","NTFS","20G"])
edriver = QtWidgets.QTreeWidgetItem(["e:\\","NTFS","30G"])
print("1.self.treeWidget.topcount=", self.treeWidget.topLevelItemCount())
self.treeWidget.addTopLevelItem(cdriver)
print( "2.self.treeWidget.topcount=",self.treeWidget.topLevelItemCount())
self.treeWidget.addTopLevelItem(ddriver)
print("3.self.treeWidget.topcount=", self.treeWidget.topLevelItemCount())
self.treeWidget.addTopLevelItem(edriver)
print("4.self.treeWidget.topcount=", self.treeWidget.topLevelItemCount())
案例输出:
1.self.treeWidget.topcount= 0
2.self.treeWidget.topcount= 1
3.self.treeWidget.topcount= 2
4.self.treeWidget.topcount= 3
本文介绍了PyQt中QTreeWidget组件的topLevelItemCount属性,该属性返回树型部件中顶层项的数量。通过实例演示了如何添加顶层项,并观察topLevelItemCount属性的变化,展示其动态更新特性。

259

被折叠的 条评论
为什么被折叠?



