python GUI庫圖形界面開發(fā)之PyQt5樹形結(jié)構(gòu)控件QTreeWidget詳細(xì)使用方法與實(shí)例
QTreeWidget 類根據(jù)預(yù)設(shè)的模型提供樹形顯示控件。
QTreeWidget 使用類似于 QListView 類的方式提供一種典型的基于 item 的樹形交互方法類,該類基于QT的“模型/視圖”結(jié)構(gòu),提供了默認(rèn)的模型來支撐 item 的顯示,這些 item 類為 QTreeWidgetItem 類。
如果不需要靈活的“模型/視圖”框架,可以使用QTreeWidget 來創(chuàng)建有層級關(guān)系的樹形結(jié)構(gòu)。當(dāng)把標(biāo)準(zhǔn) item 模型結(jié)合 QTreeView 使用時(shí),可以得到更靈活的使用方法,從而把“數(shù)據(jù)”和“顯示”分離開。
QTreeWidget類中的常用方法 方法 描述 setColumnWidth(int column,int width) 將指定列的寬度設(shè)置為給定的值 Column:指定的列 width:指定的寬度 insertTopLevelItems() 在視圖的頂層索引中引入項(xiàng)目的列表 expandAll() 展開所有節(jié)點(diǎn)的樹形節(jié)點(diǎn) invisibleRootItem() 返回樹形控件中不可見的根選項(xiàng)(Root Item) selectionItems() 返回所有選定的非隱藏項(xiàng)目的列表內(nèi) QTreeWidgetItem類中常用的方法 方法 描述 addChild() 將子項(xiàng)追加到子列表中 setText() 設(shè)置顯示的節(jié)點(diǎn)文本 Text() 返回顯示的節(jié)點(diǎn)文本 setCheckState(column.state) 設(shè)置指定列的選中狀態(tài): Qt.Checked:節(jié)點(diǎn)選中 Qt.Unchecked:節(jié)點(diǎn)沒有選中 setIcon(column,icon) 在指定的列中顯示圖標(biāo) QTreeWidget樹形結(jié)構(gòu)控件的實(shí)例樹形結(jié)構(gòu)是通過QTreeWidget和QTreeWidgetItem類實(shí)現(xiàn)的,其中QTreeWidgetItem類實(shí)現(xiàn)了節(jié)點(diǎn)的添加,其完整代碼如下
import sysfrom PyQt5.QtWidgets import *from PyQt5.QtGui import QIcon, QBrush, QColorfrom PyQt5.QtCore import Qtclass TreeWidgetDemo(QMainWindow): def __init__(self, parent=None): super(TreeWidgetDemo, self).__init__(parent) self.setWindowTitle(’TreeWidget 例子’) self.tree=QTreeWidget() #設(shè)置列數(shù) self.tree.setColumnCount(2) #設(shè)置樹形控件頭部的標(biāo)題 self.tree.setHeaderLabels([’Key’,’Value’]) #設(shè)置根節(jié)點(diǎn) root=QTreeWidgetItem(self.tree) root.setText(0,’Root’) root.setIcon(0,QIcon(’./images/root.png’)) # todo 優(yōu)化2 設(shè)置根節(jié)點(diǎn)的背景顏色 brush_red=QBrush(Qt.red) root.setBackground(0,brush_red) brush_blue=QBrush(Qt.blue) root.setBackground(1,brush_blue) #設(shè)置樹形控件的列的寬度 self.tree.setColumnWidth(0,150) #設(shè)置子節(jié)點(diǎn)1 child1=QTreeWidgetItem() child1.setText(0,’child1’) child1.setText(1,’ios’) child1.setIcon(0,QIcon(’./images/IOS.png’)) #todo 優(yōu)化1 設(shè)置節(jié)點(diǎn)的狀態(tài) child1.setCheckState(0,Qt.Checked) root.addChild(child1) #設(shè)置子節(jié)點(diǎn)2 child2=QTreeWidgetItem(root) child2.setText(0,’child2’) child2.setText(1,’’) child2.setIcon(0,QIcon(’./images/android.png’)) #設(shè)置子節(jié)點(diǎn)3 child3=QTreeWidgetItem(child2) child3.setText(0,’child3’) child3.setText(1,’android’) child3.setIcon(0,QIcon(’./images/music.png’)) #加載根節(jié)點(diǎn)的所有屬性與子控件 self.tree.addTopLevelItem(root) #TODO 優(yōu)化3 給節(jié)點(diǎn)添加響應(yīng)事件 self.tree.clicked.connect(self.onClicked) #節(jié)點(diǎn)全部展開 self.tree.expandAll() self.setCentralWidget(self.tree) def onClicked(self,qmodeLindex): item=self.tree.currentItem() print(’Key=%s,value=%s’%(item.text(0),item.text(1)))if __name__ == ’__main__’: app = QApplication(sys.argv) tree = TreeWidgetDemo() tree.show() sys.exit(app.exec_())
初始運(yùn)行圖如下
這里添加了child1的選中狀態(tài)
child1.setCheckState(0,Qt.Checked)
這里設(shè)置了根節(jié)點(diǎn)的背景顏色
brush_red=QBrush(Qt.red)
root.setBackground(0,brush_red)
brush_blue=QBrush(Qt.blue)
root.setBackground(1,brush_blue)
點(diǎn)擊,會在控制臺輸出當(dāng)前地key值與value值
self.tree.clicked.connect(self.onClicked)
def onClicked(self,qmodeLindex):
item=self.tree.currentItem()
print(’Key=%s,value=%s’%(item.text(0),item.text(1)))
在上面的例子中,QTreeWidgetItem類的節(jié)點(diǎn)是一個(gè)個(gè)添加上去的,這樣有時(shí)很不方便,特別是窗口產(chǎn)生比較復(fù)雜的樹形結(jié)構(gòu)時(shí),一般都是通過QTreeView類來實(shí)現(xiàn)的,而不是QTreeWidget類,QTreeView和QTreeWidget類最大的區(qū)別就是,QTreeView類可以使用操作系統(tǒng)提供的定制模式,比如文件系統(tǒng)盤的樹列表
import sysfrom PyQt5.QtWidgets import *from PyQt5.QtGui import *if __name__ == ’__main__’: app=QApplication(sys.argv) #window系統(tǒng)提供的模式 model=QDirModel() #創(chuàng)建一個(gè)QTreeView的控件 tree=QTreeView() #為控件添加模式 tree.setModel(model) tree.setWindowTitle(’QTreeView例子’) tree.resize(640,480) tree.show() sys.exit(app.exec_())
本文主要講解了PyQt5樹形結(jié)構(gòu)控件QTreeWidget詳細(xì)使用方法與實(shí)例,更多關(guān)于PyQt5控件使用知識請查看下面的相關(guān)鏈接
相關(guān)文章:
1. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解2. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解3. Yii2.0引入CSS,JS文件方法4. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析5. Python importlib動態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼6. vue使用webSocket更新實(shí)時(shí)天氣的方法7. 淺談python出錯(cuò)時(shí)traceback的解讀8. android studio 打包自動生成版本號與日期,apk輸入路徑詳解9. Nginx+php配置文件及原理解析10. JavaMail 1.4 發(fā)布
