← Back to list

Mastering List Boxes and Tree Views in PyQt6: Do You Know How to Use Them?

In this article, we will introduce in detail the usage, customization methods, event handling, and common problems of list boxes and tree…

Keith in Python in Plain English · 2024-10-09 04:55 · 282 claps · 4.3 min read
#python #development #pyqt6 #tree-view #programming
Open on Medium ↗
Wiki topics: 💻 · Programming

Mastering List Boxes and Tree Views in PyQt6: Do You Know How to Use Them?

In this article, we will introduce in detail the usage, customization methods, event handling, and common problems of list boxes and tree views in PyQt6, and provide complete code examples to help readers better master the use of these controls.

Photo by Boitumelo on Unsplash

Photo by Boitumelo on Unsplash

PyQt6 is a Python GUI programming toolkit based on the Qt framework. It contains many common GUI controls, such as list boxes and tree views. In this article, we will introduce in detail the usage, customization methods, event handling, and common problems of list boxes and tree views in PyQt6 and provide complete code examples to help readers master these controls better.

List Box

The list box is a common GUI control used to display a list of items in a window. In PyQt6, we can use the QListWidget class to create and manage list boxes.

How to use

The following is a simple example that demonstrates how to create a simple list box:

from PyQt6.QtWidgets import QApplication, QListWidget, QListWidgetItem

app = QApplication([])
list_widget = QListWidget()

for i in range(10):
    item = QListWidgetItem(f"Item {i+1}")
    list_widget.addItem(item)

list_widget.show()
app.exec()

In this example, we created a QListWidget object and added 10 items using the addItem() method. Finally, we displayed the list box using the show() method.

Custom methods

In the list box, we can use the QListWidgetItem class to customize the appearance and behavior of each item. The following is an example that shows how to set the color and font of each item:

from PyQt6.QtGui import QFont, QColor
from PyQt6.QtWidgets import QApplication, QListWidget, QListWidgetItem

app = QApplication([])
list_widget = QListWidget()

for i in range(10):
    item = QListWidgetItem(f"Item {i+1}")
    item.setForeground(QColor(255, 0, 0))
    font = QFont('Arial', 16)
    item.setFont(font)
    list_widget.addItem(item)

list_widget.show()
app.exec()

In this example, we use the setForeground() method and the setFont() method to set the color and font of each item.

Event Handling

We can use event handling in a list box to respond to user actions. The following is an example that shows how to display the text content of an item in a list box when the user clicks it:

from PyQt6.QtWidgets import QApplication, QListWidget, QListWidgetItem

app = QApplication([])

def handle_item_click(item):
    print(item.text())

list_widget = QListWidget()
list_widget.itemClicked.connect(handle_item_click)

for i in range(10):
    item = QListWidgetItem(f"Item {i+1}")
    list_widget.addItem(item)

list_widget.show()
app.exec()

In this example, we use the itemClicked signal to handle the event of a user clicking an item in the list box. The handle_item_click() function is called after the user clicks an item and prints the text content of the item.

How to disable items in a listbox?

In a list box, we can use the setEnabled() method to disable or enable items. The following is an example that shows how to disable the first item:

from PyQt6.QtWidgets import QApplication, QListWidget, QListWidgetItem

app = QApplication([])
list_widget = QListWidget()

for i in range(10):
    item = QListWidgetItem(f"Item {i+1}")
    list_widget.addItem(item)

list_widget.item(0).setEnabled(False)

list_widget.show()
app.exec()

In this example, we use the setEnabled() method to disable the first item in the list box.

Tree View

The tree view is a common GUI control used to display hierarchical data in a window. In PyQt6, we can use the QTreeView class to create and manage tree views.

How to use

The following is a simple example that demonstrates how to create a simple tree view:

from PyQt6.QtCore import QStringListModel
from PyQt6.QtWidgets import QApplication, QTreeView

app = QApplication([])
tree_view = QTreeView()

model = QStringListModel()
model.setStringList(['Item 1', 'Item 2', 'Item 3'])
tree_view.setModel(model)

tree_view.show()
app.exec()

In this example, we create a QTreeView object and set the data model using the setModel() method. The data model is created using the QStringListModel class and contains three items.

Custom methods

In a tree view, we can use the QStandardItemModel class to customize the appearance and behavior of each item. The following is an example that shows how to set the color and font of each item:

from PyQt6.QtGui import QFont, QColor
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QTreeView, QStandardItem, QStandardItemModel

app = QApplication([])
tree_view = QTreeView()

model = QStandardItemModel()
root_item = model.invisibleRootItem()
for i in range(3):
    item = QStandardItem(f"Item {i+1}")
    item.setForeground(QColor(255, 0, 0))
    font = QFont('Arial', 16)
    item.setFont(font)
    item.setCheckable(True)
    item.setTristate(True)
    item.setEditable(True)
    item.setData(Qt.UserRole, f"Custom data {i+1}")
    root_item.appendRow(item)

tree_view.setModel(model)

tree_view.show()
app.exec()

In this example, we use the QStandardItem class to create each item and use the setForeground() method, setFont() method, setCheckable() method, setTristate() method, setEditable() method, and setData() method to set the color, font, selection state, editable state, and custom data of each item.

Event Handling

In a tree view, we can use event handling to respond to user actions. The following is an example that shows how to display the text content and custom data of an item in a tree view when a user clicks on it:

from PyQt6.QtCore import Qt
from PyQt6.QtGui import QStandardItemModel
from PyQt6.QtWidgets import QApplication, QTreeView

app = QApplication([])

def handle_item_click(index):
    item = model.itemFromIndex(index)
    print(item.text())
    print(item.data(Qt.UserRole))

tree_view = QTreeView()
tree_view.clicked.connect(handle_item_click)

model = QStandardItemModel()
root_item = model.invisibleRootItem()
for i in range(3):
    item = QStandardItem(f"Item {i+1}")
    item.setData(Qt.UserRole, f"Custom data {i+1}")
    root_item.appendRow(item)

tree_view.setModel(model)

tree_view.show()
app.exec()

In this example, we use the clicked signal to handle the event of a user clicking an item in the tree view. The handle_item_click() function is called after the user clicks an item and prints the text content and custom data of the item.

How to add sub-projects?

In a tree view, we can use the appendRow() method to add subitems to an item. The following is an example that shows how to add two subitems to the first item:

from PyQt6.QtGui import QStandardItemModel, QStandardItem
from PyQt6.QtWidgets import QApplication, QTreeView

app = QApplication([])
tree_view = QTreeView()

model = QStandardItemModel()
root_item = model.invisibleRootItem()
for i in range(3):
    item = QStandardItem(f"Item {i+1}")
    root_item.appendRow(item)
    if i == 0:
        for j in range(2):
            child_item = QStandardItem(f"Child item {j+1}")
            item.appendRow(child_item)

tree_view.setModel(model)

tree_view.show()
app.exec()

In this example, we have added two subitems to the first item using the appendRow() method.

Conclusion

List boxes and tree views are commonly used GUI controls in PyQt6 and can be used to display various data. I hope that readers can better master the use of list boxes and tree views in PyQt6 through the study of this article.

In Plain English 🚀

Thank you for being a part of the **In Plain English** community! Before you go:


메타데이터
post_id
ec6522771bcb
slug
mastering-list-boxes-and-tree-views-in-pyqt6-do-you-know-how-to-use-them-ec6522771bcb
url
https://python.plainenglish.io/mastering-list-boxes-and-tree-views-in-pyqt6-do-you-know-how-to-use-them-ec6522771bcb
canonical_url
https://python.plainenglish.io/mastering-list-boxes-and-tree-views-in-pyqt6-do-you-know-how-to-use-them-ec6522771bcb
author_url
https://medium.com/@xuezhongyu01
status
ok
fetched_at
2026-06-29 22:44:20