久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

python GUI庫圖形界面開發(fā)之PyQt5樹形結(jié)構(gòu)控件QTreeWidget詳細(xì)使用方法與實(shí)例

瀏覽:4日期:2022-08-04 18:29:40
PyQt5樹形結(jié)構(gòu)控件QTreeWidget簡介

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)行圖如下

python GUI庫圖形界面開發(fā)之PyQt5樹形結(jié)構(gòu)控件QTreeWidget詳細(xì)使用方法與實(shí)例

優(yōu)化一:設(shè)置節(jié)點(diǎn)的狀態(tài)

這里添加了child1的選中狀態(tài)

 child1.setCheckState(0,Qt.Checked)

python GUI庫圖形界面開發(fā)之PyQt5樹形結(jié)構(gòu)控件QTreeWidget詳細(xì)使用方法與實(shí)例

優(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)

python GUI庫圖形界面開發(fā)之PyQt5樹形結(jié)構(gòu)控件QTreeWidget詳細(xì)使用方法與實(shí)例

優(yōu)化三:給節(jié)點(diǎn)添加響應(yīng)事件

點(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)))

python GUI庫圖形界面開發(fā)之PyQt5樹形結(jié)構(gòu)控件QTreeWidget詳細(xì)使用方法與實(shí)例

系統(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 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_())

python GUI庫圖形界面開發(fā)之PyQt5樹形結(jié)構(gòu)控件QTreeWidget詳細(xì)使用方法與實(shí)例

本文主要講解了PyQt5樹形結(jié)構(gòu)控件QTreeWidget詳細(xì)使用方法與實(shí)例,更多關(guān)于PyQt5控件使用知識請查看下面的相關(guān)鏈接

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 欧美欧美欧美 | 国产第99页| 特黄特黄aaaa级毛片免费看 | 日本一本视频 | 久久精品国产v日韩v亚洲 | 精品国产乱码久久久久久88av | 成人片免费看 | 日韩在线免费电影 | 精品久久久久国产免费 | 丁香五月网久久综合 | 91国内精品 | 91久久久久 | 中文字幕在线观看不卡视频 | 日本黄色片免费看 | 精品久久久久久久久久久久久久 | 日本一区二区不卡 | 在线播放中文字幕 | 懂色av一区二区三区在线播放 | 高清国产午夜精品久久久久久 | 免费视频一区二区 | 国产精品不卡视频 | 7777av| 欧美日本免费一区二区三区 | 日韩av一区二区三区在线 | 欧美一区二区三区免费 | 激情综合久久 | 武道仙尊动漫在线观看 | 精品久久99 | 日韩精品在线一区 | 午夜午夜精品一区二区三区文 | 国产精品免费一区二区三区四区 | 亚洲欧美日韩另类精品一区二区三区 | 亚洲精品一二三四五区 | 97夜夜操 | 国产精品第一国产精品 | 日本久久久影视 | 欧美成人免费视频 | 成人欧美日韩一区二区三区 | 久久久久久av | av在线入口| 91免费观看 |