PyQt(Python+Qt)学习随笔:QTreeWidget的topLevelItemCount属性

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

老猿Python,跟老猿学Python!

原文地址:https://www.cnblogs.com/LaoYuanPython/p/12570522.html