Day 28 | learn PyQt5, QTreeWidget of advanced control

The tree structure is implemented through QTreeWidget and qtreewidgettitem classes, in which qtreewidgettitem class implements the addition of nodes. QTreeWidget is a hierarchical nested structure. The outer and inner layers of a tree structure have similar structures. It is often used to represent a data structure with one upper layer and multiple lower layers.

In the structure, the root node of the tree has no precursor node, and each other node has and has only one precursor node. Leaf nodes have no subsequent nodes, and the number of subsequent nodes of each other node can be one or multiple. The common methods are shown in the table below:

method

describe

setColumnWidth(int column,int width)

Set the width of the column. Column specifies the subscript of the column, the subscript of column 1 is 0, and width specifies the width of the column;

insertTopLevelItems()

Introduce a list of items into the top-level index of the view;

expandAll()

Expand the tree node of all nodes;

invisibleRootItem()

Returns the invisible root option in the tree control;

selectionItems()

Returns the list items of all selected non hidden items;

The common methods of QTreeWidgetItem are shown in the following table:

method

describe

addChild()

Append sub items to the sub list;

setText()

Set the text content of the node;

text()

Get the text value of the node;

setCheckState(int column,int state)

Set the selected status of the specified column. When the state is Qt.Checked, it means that the node is selected. When it is Qt.Unchecked, the node is not selected;

setIcon(int column,

QIcon icon)

Set the display icon of the specified column. Column is the subscript of the column and icon is the icon.

Program list: tree.py

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, 
  QHBoxLayout, QTreeWidget, QTreeWidgetItem
from PyQt5.QtGui import QIcon


# Inherit QWidget
class TreeWidget(QWidget):
    tree = None

    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        # Set layout
        layout = QHBoxLayout()
        # Declare table control
        self.tree = QTreeWidget()
        self.tree.setHeaderLabels(["Key", "Value"])
        # Set the width of the column of the tree control, column 1
        self.tree.setColumnWidth(0, 160)
        # Set root node
        root = QTreeWidgetItem(self.tree)
        root.setText(0, "Mobile phone system")
        root.setIcon(0, QIcon("root.png"))
        # Set child node 1
        child1 = QTreeWidgetItem()
        child1.setText(0, "iOS")
        child1.setText(1, "Iphone")
        child1.setIcon(0, QIcon("ios.png"))
        root.addChild(child1)
        # Set child node 2
        child2 = QTreeWidgetItem()
        child2.setText(0, "Android")
        child2.setText(1, "Huawei")
        child2.setIcon(0, QIcon("android.png"))
        root.addChild(child2)
        # Expand all nodes
        self.tree.expandAll()
        layout.addWidget(self.tree)
        # Click event
        self.tree.clicked.connect(self.tree_click)
        self.setLayout(layout)
        # resize window
        self.resize(900, 500)
        # Center window
        self.center()
        # Window title
        self.setWindowTitle("QTreeWidget application")
        # Display window
        self.show()
        # Get folder path

    def tree_click(self):
        item = self.tree.currentItem()
        print("Key=%s,value=%s" % (item.text(0), item.text(1)))

    # Achieve centering
    def center(self):
        f = self.frameGeometry()
        c = QDesktopWidget().availableGeometry().center()
        f.moveCenter(c)
        self.move(f.topLeft())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = TreeWidget()
    sys.exit(app.exec_())

After running the program, the pop-up window is as follows:

Well, that's all for QTreeWidget. Pay attention to me and the next section will be more exciting.

Today's headline: Lao Chen said that Python will be fully shared by 2021 national day. The complete courses include:
1. 12 days
2. Data analysis in 16 days
3. Python web crawler in 10 days
four   Django3.0 project practice
five   Wxpython in 25 days
six   The 28 day society PyQt5 is being released
seven   The 25 day society Seaborn data analysis was released on csdn
eight   "3-day Pyecharts data analysis" was released during the national day

Keywords: Python Data Analysis PyQt5

Added by fangfang on Mon, 04 Oct 2021 23:22:54 +0300