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