python GUI庫(kù)圖形界面開發(fā)之PyQt5樹形結(jié)構(gòu)控件QTreeWidget詳細(xì)使用方法與實(shí)例
PyQt5樹形結(jié)構(gòu)控件QTreeWidget簡(jiǎn)介
QTreeWidget 類根據(jù)預(yù)設(shè)的模型提供樹形顯示控件。
QTreeWidget 使用類似于 QListView 類的方式提供一種典型的基于 item 的樹形交互方法類,該類基于QT的“模型/視圖”結(jié)構(gòu),提供了默認(rèn)的模型來支撐 item 的顯示,這些 item 類為 QTreeWidgetItem 類。
如果不需要靈活的“模型/視圖”框架,可以使用QTreeWidget 來創(chuàng)建有層級(jí)關(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 sys from PyQt5.QtWidgets import * from PyQt5.QtGui import QIcon, QBrush, QColor from PyQt5.QtCore import Qt class 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)行圖如下
優(yōu)化一:設(shè)置節(jié)點(diǎn)的狀態(tài)
這里添加了child1的選中狀態(tài)
child1.setCheckState(0,Qt.Checked)
優(yōu)化二:設(shè)置節(jié)點(diǎn)的背景顏色
這里設(shè)置了根節(jié)點(diǎn)的背景顏色
brush_red=QBrush(Qt.red)
root.setBackground(0,brush_red)
brush_blue=QBrush(Qt.blue)
root.setBackground(1,brush_blue)
優(yōu)化三:給節(jié)點(diǎn)添加響應(yīng)事件
點(diǎn)擊,會(huì)在控制臺(tái)輸出當(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)))
系統(tǒng)定制模式實(shí)例
在上面的例子中,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 sys from 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控件使用知識(shí)請(qǐng)查看下面的相關(guān)鏈接
相關(guān)文章
Python之inspect模塊實(shí)現(xiàn)獲取加載模塊路徑的方法
今天小編就為大家分享一篇Python之inspect模塊實(shí)現(xiàn)獲取加載模塊路徑的方法,具有很好的價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10Numpy中如何創(chuàng)建矩陣并等間隔抽取數(shù)據(jù)
這篇文章主要介紹了Numpy中如何創(chuàng)建矩陣并等間隔抽取數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Python實(shí)現(xiàn)CNN的多通道輸入實(shí)例
今天小編就為大家分享一篇Python實(shí)現(xiàn)CNN的多通道輸入實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01python用dataframe將csv中的0值數(shù)據(jù)轉(zhuǎn)化為nan缺失值字樣
本文主要介紹了python用dataframe將csv中的0值數(shù)據(jù)轉(zhuǎn)化為nan缺失值字樣,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08