[QtQuick] ItemSelectionModel for custom QAstractTableModel
Unsolved
Qt for Python
-
I have this
main.py
with a customQAbstractTableModel
:import sys import signal from PyQt6.QtGui import QGuiApplication from PyQt6.QtQml import QQmlApplicationEngine, qmlRegisterSingletonInstance from PyQt6.QtCore import Qt, QAbstractTableModel class InstalledPkgsModel(QAbstractTableModel): def __init__(self, data): super(InstalledPkgsModel, self).__init__() self._data = data def data(self, index, role): if role == Qt.ItemDataRole.DisplayRole: value = self._data[index.row()][index.column()] return value def rowCount(self, index): return len(self._data) def columnCount(self, index): try: return len(self._data[0]) # If there are no installed mods in the prefix except IndexError: return 1 # Make app respond to Ctrl-C signal.signal(signal.SIGINT, signal.SIG_DFL) app = QGuiApplication(sys.argv) engine = QQmlApplicationEngine() engine.quit.connect(app.quit) # type: ignore # Populate manage table view installed packages data = [ ["git", "", "common", "1.0"], ["distutils", "", "common", "1.0"], ["bsa", "", "common", "0.4"], ["nexus", "", "common", "1.0"], ["fallout_4", "", "common", "0.1"], ] installed_pkgs_model = InstalledPkgsModel(data) qmlRegisterSingletonInstance( "installed_pkgs_model", 1, 0, "InstalledPkgsModel", installed_pkgs_model ) engine.load("main.qml") sys.exit(app.exec())
I'd like to set something up where I can select rows in the
TableView
that's using this model. I currently have thismain.qml
:import QtQuick import QtQuick.Controls import QtQuick.Layouts import installed_pkgs_model 1.0 ApplicationWindow { visible: true width: 1000 height: 700 title: "Portmod" TableView { id: installedPkgsTable width: 1000 height: 700 columnSpacing: 1 rowSpacing: 1 clip: true model: InstalledPkgsModel selectionModel: ItemSelectionModel { model: installedPkgsTable.model } delegate: Rectangle { required property bool selected implicitWidth: 300 implicitHeight: 50 color: selected ? "blue" : "lightgray" Text { text: display } } } }
The problem is, it doesn't work. Clicking on a row does nothing.
-
Hey,
can you share how you are exposing theQAbstractTableModel
(in Python) to your QML file? (better if you can share the full code :P ) -
@CristianMaureira Sure! I've updated the main post to be a MRE.
-
@CristianMaureira Was the MRE helpful?