Pyqt5 QTreeWidget使用

在使用pyqt5编写UI时,QTreeWidget对象可以提供预定义模型的树形视图显示。QT文档对QTreeWidget定义如下:

The QTreeWidget class is a convenience class that provides a standard tree widget with a classic item-based interface similar to that used by the QListView class in Qt 3. This class is based on Qt's Model/View architecture and uses a default model to hold items, each of which is a QTreeWidgetItem.

Developers who do not need the flexibility of the Model/View framework can use this class to create simple hierarchical lists very easily. A more flexible approach involves combining a QTreeView with a standard item model. This allows the storage of data to be separated from its representation.

1.QTreeWidget的实例化

treeWidget = QtWidgets.QTreeWidget(parent)

如在MainWindow下添加QTreeWidget:

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.treeWidget = QtWidgets.QTreeWidget(self.centralwidget)
        self.treeWidget.setObjectName("treeWidget")

2.QTreeWidget的header

header通过self.treeWidget.headerItem()返回,是QTreeWidgetItem类型的对象

self.treeWidget.headerItem().setText(0,"key")可以设置header显示名称

3.QTreeWidget创建子树与子节点

root1=QtWidgets.QTreeWidgetItem(self.treeWidget) #QTreeWidgetItem object: root 
root1.setText(0,root_name) #set text of root1
para_list=["alpha","beta","gama"]
for i in range(len(para_list)):
    child=QtWidgets.QTreeWidgetItem(root)  #child of root
    child.setText(0,para_list[i])
root2...

QtreeWidgetItem(parent)在treeWidget下创建一个根节点,并进一步在根节点下创建多个子节点

QtreeWidgetItem.setText(column,text)在某列设置其显示字符串,如setText(0,"alpha")表示第一列显示alpha

QtreeWidgetItem.setFlags(QtCore.Qt.ItemFlags)可以设置Item的flag,如可选择,可编辑等

Constant Value Description
Qt::NoItemFlags 0 It does not have any properties set.
Qt::ItemIsSelectable 1 It can be selected.
Qt::ItemIsEditable 2 It can be edited.
Qt::ItemIsDragEnabled 4 It can be dragged.
Qt::ItemIsDropEnabled 8 It can be used as a drop target.
Qt::ItemIsUserCheckable 16 It can be checked or unchecked by the user.
Qt::ItemIsEnabled 32 The user can interact with the item.
Qt::ItemIsTristate 64 The item is checkable with three separate states.

root.setFlags(QtCore.Qt.NoItemFlags)
NoItemFlags表示这个Item没有任何属性

4.QTreeWidget节点的选择

QTreeWidget默认为单选节点,如需要设置多选节点可以通过treeWidget.setSelectionMode(mode)设置

可以设置的选择模式如下表所示,其中最常用的是SingleSelection 和 ExtendedSelection.

Constant Value Description
QAbstractItemView::SingleSelection 1 When the user selects an item, any already-selected item becomes unselected. It is possible for the user to deselect the selected item.
QAbstractItemView::ContiguousSelection 4 When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item.
QAbstractItemView::ExtendedSelection 3 When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Ctrl key when clicking on an item, the clicked item gets toggled and all other items are left untouched. If the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item. Multiple items can be selected by dragging the mouse over them.
QAbstractItemView::MultiSelection 2 When the user selects an item in the usual way, the selection status of that item is toggled and the other items are left alone. Multiple items can be toggled by dragging the mouse over them.
QAbstractItemView::NoSelection 0 Items cannot be selected.

选择的节点可以通过self.treeWidget.selectedItems()返回选择的Item列表,列表中每个Item都是QTreeWidgetItem对象。Item.text(0)为选中item column 0的内容。

Item_list=self.treeWidget.selectedItems()  #return the selected item as a list
for ii in Item_list:
    print ii.text(0)

5.signal 和 slot

QTreeWidget可以使用信号与槽机制实现QT下的对象通信,以鼠标单击触发为例:

self.treeWidget.itemClicked['QTreeWidgetItem*','int'].connect(MainWindow.getitem)

将itemClicked信号连接到getitem() slot函数

def getitem(self, item,column):
    Item_list=self.treeWidget.selectedItems()
    for ii in Item_list:
      print ii.text(0)



你可能感兴趣的:(Pyqt5 QTreeWidget使用)