目录
- 1 官方帮助文档
- 2 基本使用
- 3 模型事件
1 官方帮助文档
QTreeWidget Qt5.15帮助文档
2 基本使用
#!/usr/bin/python3
# -*- coding: GBK -*-
import sys
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import *
class QTreeWidgetDemo(QWidget):
def __init__(self):
super(QTreeWidgetDemo, self).__init__()
self.resize(400, 400)
self.mainlayout = QVBoxLayout(self)
self.treewidget = QTreeWidget(self)
self.mainlayout.addWidget(self.treewidget)
self.setWindowTitle('树控件 QTreeWidget')
#指定列数 标签头
self.treewidget.setColumnCount(3)
self.treewidget.setHeaderLabels(['姓名','年龄','籍贯'])
#根节点
self.root = QTreeWidgetItem(self.treewidget)
self.root.setText(0, '根节点')
#添加子节点
self.childitem = QTreeWidgetItem(self.root)
self.childitem.setText(0,'子节点文字')
if __name__ == '__main__':
app = QApplication(sys.argv)
test = QTreeWidgetDemo()
test.show()
sys.exit(app.exec_())
如下:
#!/usr/bin/python3
# -*- coding: GBK -*-
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from images import *
class QTreeWidgetDemo(QWidget):
def __init__(self):
super(QTreeWidgetDemo, self).__init__()
self.resize(400, 400)
self.mainlayout = QVBoxLayout(self)
self.treewidget = QTreeWidget(self)
self.mainlayout.addWidget(self.treewidget)
self.setWindowTitle('树控件 QTreeWidget')
#指定列数 标签头
self.treewidget.setColumnCount(3)
self.treewidget.setHeaderLabels(['姓名','年龄','籍贯'])
#根节点
self.root = QTreeWidgetItem(self.treewidget)
self.root.setIcon(0, QIcon(":/questionMark.png"))
self.root.setIcon(1, QIcon(":/whole.png"))
self.root.setText(0, '根节点')
self.root.setText(1, '第一列')
self.root.setText(2, '第二列')
topitem1 = QTreeWidgetItem(self.treewidget )
self.treewidget.addTopLevelItem(topitem1)
topitem1.setText(0, '#第0列')
childitem = QTreeWidgetItem(topitem1)
childitem.setText(0,'childitem0')
childitem.setText(1,'childitem1')
#itemClicked(QTreeWidgetItem *item, int column)
self.treewidget.itemClicked.connect(self.itemclicked)
topitem2 = QTreeWidgetItem(self.treewidget, ['0','1','2'])
def itemclicked(self, item, column):
print("点击", column, item.text(column))
if __name__ == '__main__':
app = QApplication(sys.argv)
test = QTreeWidgetDemo()
test.show()
sys.exit(app.exec_())
如下:
3 模型事件
#!/usr/bin/python3
# -*- coding: GBK -*-
import sys
from PyQt5.QtCore import QSize
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from images import *
class QTreeViewDemo(QWidget):
def __init__(self):
super(QTreeViewDemo, self).__init__()
self.resize(400, 400)
self.mainlayout = QVBoxLayout(self)
self.treeview = QTreeView(self)
self.mainlayout.addWidget(self.treeview)
self.setWindowTitle('QTreeView')
self.mode = QStandardItemModel(self)
self.mode.setHorizontalHeaderLabels(['列'])
self.treeview.setModel(self.mode)
#隐藏表头
self.treeview.setHeaderHidden(True)
#展开
self.treeview.expandAll()
item00 = QStandardItem('第一行')
item10 = QStandardItem('第二行')
item20 = QStandardItem('第三行')
item00.setIcon(QIcon(":/whole.png"))
item00.setEditable(False)
item00.setBackground(QBrush(QColor(0,240,0)))
item10.setIcon(QIcon(":/whole.png"))
item20.setIcon(QIcon(":/whole.png"))
self.mode.setItem( 0, 0, item00 )
self.mode.setItem( 1, 0, item10 )
self.mode.setItem( 2, 0, item20 )
#点击信号
self.treeview.clicked.connect(self.item_clicked)
def item_clicked(selfself, index):
print("---",index.row(), index.column() )
if __name__ == '__main__':
app = QApplication(sys.argv)
test = QTreeViewDemo()
test.show()
sys.exit(app.exec_())
如下: