I made a model that adds data to a QTreeView. I want to add a widget (eg: QProgressbar) as a child element to each row within that QTreeView. So that every time if we click on each row we can see the progressbar as a childItem for that row
main.py
import sys
from PySide2.QtWidgets import QApplication, QMainWindow, QHeaderView, QPushButton
from PySide2 import QtCore
from TreeViewUI import Ui_MainWindow
from custom_models.table_model import CustomTableModel
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, data):
QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.table_model = CustomTableModel(data)
self.treeView.setIconSize(QtCore.QSize(360 / 4, 640 / 4))
self.treeView.header().setSectionResizeMode(QHeaderView.ResizeToContents)
self.treeView.setModel(self.table_model)
def get_data():
content_list = [
{
"user_id": 101,
"user_name": "User_1",
"user_image": "images/demo.jpg",
"user_details": "details_1",
"user_progress": 45,
},
{
"user_id": 102,
"user_name": "User_2",
"user_image": "images/demo.jpg",
"user_details": "details_2",
"user_progress": 33,
},
{
"user_id": 103,
"user_name": "User_3",
"user_image": "images/demo.jpg",
"user_details": "details_3",
"user_progress": 80,
},
]
return content_list
if __name__ == '__main__':
app = QApplication([])
data = get_data()
main_window = MainWindow(data)
main_window.showMaximized()
sys.exit(app.exec_())
table_model.py
from PySide2.QtCore import Qt, QAbstractTableModel, QModelIndex
from PySide2.QtGui import QColor, QImage, QIcon, QPixmap
class CustomTableModel(QAbstractTableModel):
def __init__(self, data):
QAbstractTableModel.__init__(self)
self.content_id = []
self.content_names = []
self.content_url = []
self.content_variant = []
self.content_progress = []
for content_data in data:
self.content_id.append(content_data["user_id"])
self.content_names.append(content_data["user_name"])
self.content_url.append(content_data["user_image"])
self.content_variant.append(content_data["user_details"])
self.content_progress.append(content_data["user_progress"])
self.row_count = len(self.content_id)
self.column_count = 4
def rowCount(self, parent=QModelIndex()):
return self.row_count
def columnCount(self, parent=QModelIndex()):
return self.column_count
def headerData(self, section, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return ("ID", "NAME", "IMAGE PREVIEW", "DETAILS")[section]
def data(self, index, role=Qt.DisplayRole):
column = index.column()
row = index.row()
if role == Qt.DisplayRole:
if column == 0:
content_id = self.content_id[row]
return content_id
elif column == 1:
return self.content_names[row]
elif column == 3:
return self.content_variant[row]
elif role == Qt.DecorationRole:
if column == 2:
image_path = self.content_url[row]
image = QImage()
image.load(image_path)
icon = QIcon()
icon.addPixmap(QPixmap.fromImage(image))
return icon
elif role == Qt.TextAlignmentRole:
return Qt.AlignLeft
TreeViewUI.py
from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
QRadialGradient)
from PySide2.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(817, 600)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.gridLayout = QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(u"gridLayout")
self.verticalLayout = QVBoxLayout()
self.verticalLayout.setObjectName(u"verticalLayout")
self.label = QLabel(self.centralwidget)
self.label.setObjectName(u"label")
self.verticalLayout.addWidget(self.label)
self.treeView = QTreeView(self.centralwidget)
self.treeView.setObjectName(u"treeView")
self.verticalLayout.addWidget(self.treeView)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.label.setText(QCoreApplication.translate("MainWindow", u"TreeView", None))
# retranslateUi
These are the files I created to test for a sample implementation for QTreeView. The basic structure of the Treeview would be as follows:
User_ID User_Name User_Image User_Details
101 ABC abc.jpg abc
QProgressBar widget for the above user(101)# QProgressbar as the childItem for each row.
102 DEF def.jpg def
QProgressBar widget for the above user(102)
103 GHI ghi.png ghi
QProgressBar widget for the above user(103)