Python

Showing multi-column items in a PySide combo box

A combo box can display a table: give it a model and a QTableView, and each row shows several columns.

sf
Naoki « segfault »
systèmes & back-end · updated Jan 2025
This guide revisits and updates an original tutorial from noiretaya.com (log.noiretaya.com/261). The code has been refreshed for current versions.

Model + custom view

By default a QComboBox shows one column. Feed it a QStandardItemModel with several columns and swap its popup for a QTableView.

from PySide6.QtWidgets import QComboBox, QTableView
from PySide6.QtGui import QStandardItemModel, QStandardItem

model = QStandardItemModel(0, 2)
model.setHorizontalHeaderLabels(["Code", "Label"])
for code, label in [("FR","France"), ("JP","Japan"), ("US","United States")]:
    model.appendRow([QStandardItem(code), QStandardItem(label)])

combo = QComboBox()
combo.setModel(model)
combo.setView(QTableView())
combo.setModelColumn(1)           # which column is shown when collapsed

Polishing the popup

view = combo.view()
view.verticalHeader().setVisible(False)
view.setSelectionBehavior(QTableView.SelectRows)
view.resizeColumnsToContents()
Why a model: separating data (model) from display (view) means you can reuse the same data in a table elsewhere, and sorting/filtering comes for free.