A detailed explanation of the implementation of tree structure in pyqt5

Time:2020-1-17

The essence of the tree is that many pieces of data are displayed in layers according to certain internal relations. Therefore, each piece of data includes data items and interrelations. The data item corresponds to the column in the tree, and the relationship corresponds to which item should be displayed.

In pyqt5, there are two forms of tree implementation, among which the simpler is to use the tree widget control.

For static data, to implement tree structure, you can drag a tree widget control directly into QT, then right-click it and select Edit.

Column is the number of data items in each piece of data. Item reflects the relationship between data and data.

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

One of the feasible implementation methods is to use SQL to sort the data found according to the tree structure from top to bottom, and then use the following code to implement:

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

With the above code, you get a tree with only display function.

In general, we hope that we can click an item in the tree and have corresponding operations.

Enter the editing interface of signal and slot, left click tree widget, drag to the blank of form, and the editing interface of signal and slot appears.

On the left is the list of tree widget signals, and on the right is our customized slot. You can click Edit to add and delete.

For the signal description of tree widget, please refer to QT’s help document. Here is a 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 editing, in the PY file generated by the UI file, the code to realize the signal and slot connection is as follows:

self.treeWidget.itemClicked['QTreeWidgetItem*','int'].connect(Form.tree_item_click)
#Where tree item click is a self defined slot function

The parameters of the slot function and the signal are the same or less.


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

Item is the qtreewidgettitem class. You can refer to the description of the qtreewidgettitem class in the QT help document to implement the user-defined function.

Conclusion:This paper provides a tree structure implementation, the response of a tree item after being clicked. More importantly, it provides a query method for pyqt related data. The query of QT help documents can guide the application of pyqt.

The above detailed explanation of the implementation method of tree structure in pyqt5 is all the content shared by the editor. I hope to give you a reference and support developepaer.