SoFunction
Updated on 2024-10-30

Detailed explanation of the implementation of tree structures in PyQt5

The essence of a tree is that many pieces of data are displayed hierarchically according to certain internal relationships. Therefore, each piece of data consists of a data item and a correlation. The data item corresponds to the column in the tree, and the correlation corresponds to the entry under which it should be displayed.

There are two forms of tree implementation in PyQt5, the simpler of which uses the Tree Widget control.

For static data, implementing a tree structure can be done directly by dragging in a Tree Widget control in Qt, then right-clicking on it and choosing Edit.

Where column is how many data items there are for each piece of data, Item reflects the data and what is the relationship between the data.

For the data we need to find out from the database, we need to arrange the data according to the actual data and determine how many entries there are at each level of a tree.

One of the feasible implementations is to be constrained to use SQL to sort the looked-up data according to the tree structure from top to bottom, and then implement it with the following code:

a = ['1', '11', '12', '13', '2', '21', '22', '23']
# Your data follow
# 【root,child1,child11,child12,child13,child2,child21,child22】
# In this order
for i in range(len(a)):
  if len(a[i]) == 1 :
    root = ()
    (0,a[i])
   elif len(a[i]) == 2 :
    child = (root)
    (0,a[i])

What you get with the above code is a tree that is just a display only.

In general, we want our click on an entry in the tree to have the corresponding action.

To get to the editing screen of signals and slots, left click on the Tree Widget and drag it to the blank space of the Form to bring up the editing screen of signals and slots.

On the left is a list of the Tree Widget's signals, and on the right are our customized slots that can be added and removed by clicking Edit.

The signals of the Tree Widget can be described in the QT help file. The following is the description of the itemClicked signal:

[signal] void QTreeWidget::itemClicked(QTreeWidgetItem *item, int column)

This signal is emitted when the user clicks inside the widget.

The specified item is the item that was clicked. The column is the item's column that was clicked. If no item was clicked, no signal will be emitted.

After the above edits, the py file generated by the ui file contains the following code that implements the signal and slot connections:

['QTreeWidgetItem*','int'].connect(Form.tree_item_click)
# included among thesetree_item_clickis a self-defined slot function

The parameters of the slot function should be the same or less than those of the signal.

def tree_item_click(self,item,n):
    print((n))

item is the QTreeWidgetItem class. You can refer to the QTreeWidgetItem class description in the QT help file to implement customized functions.

Summary:This article provides an implementation of a tree structure, an implementation of a response after an entry in the tree is clicked. More over, it provides a way to query PyQt related information. By querying the QT help documentation, the application of PyQt can be guided.

Above this on PyQt5 in the tree structure of the realization of the method of detail is all that I share with you, I hope to be able to give you a reference, and I hope that you support me more.