How can I add headers (country, state, city) to a QTreeView
from PyQt6.QtWidgets import QMainWindow, QApplication, QTreeView
import sys
from PyQt6.QtGui import QStandardItem, QColor, QFont, QStandardItemModel
class StandardItem(QStandardItem):
def init(self, txt=“”, font_size=12, set_bold=False, color=QColor(0,0,0)):
super().init()
fnt = QFont(“open_sanse”, font_size)
fnt.setBold(set_bold)
self.setEditable(False)
self.setForeground(color)
self.setFont(fnt)
self.setText(txt)
class MainWindow(QMainWindow):
def init(self):
super().init()
self.setWindowTitle("tree view test")
self.resize(500, 300)
treeView = QTreeView()
treeView.setHeaderHidden(True)
treeModel = QStandardItemModel()
rootNode = treeModel.invisibleRootItem()
america = StandardItem("America", 16, set_bold=True)
california = StandardItem("California", 14)
america.appendRow(california)
texas = StandardItem("Texas", 14)
america.appendRow(texas)
okland = StandardItem("Okland", 12, color=QColor(155,0,0))
sanfransisco = StandardItem("Sanfransisco", 12, color=QColor(155,0,0))
sanjose = StandardItem("San jose", 12, color=QColor(155,0,0))
california.appendRow(okland)
california.appendRow(sanfransisco)
california.appendRow(sanjose)
austin = StandardItem("Austin", 1, color=QColor(155,0,0))
houston = StandardItem("Houston", 1, color=QColor(155,0,0))
dalas = StandardItem("Dalas", 1, color=QColor(155,0,0))
texas.appendRow(austin)
texas.appendRow(houston)
texas.appendRow(dalas)
canada = StandardItem("Canada", 16, set_bold=True)
ontario = StandardItem("Ontario", 14)
bc = StandardItem("British Columbia", 14)
alberta = StandardItem("Alberta", 14)
canada.appendRows([alberta, bc, ontario])
rootNode.appendRow(america)
rootNode.appendRow(canada)
treeView.setModel(treeModel)
treeView.expandAll()
treeView.doubleClicked.connect(self.getValue)
self.setCentralWidget(treeView)
def getValue(self, val):
print(val.data())
print(val.row())
print(val.column())
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()