I want to display a subset of columns from an large existing C++ TableModel
in a new QML TableView
.
All the documenation and examples I've read override QAbstractItemModel::roleNames()
to expose model data associated with the roles as properties in QML.
The problem is that the legacy TableModel
's data()
function has a complex implementation tightly coupled to Qt::DisplayRole
:
\...
QVariant LegacyModel::data(const QModelIndex& index, int role) {
switch(role) {
\...
case Qt::DisplayRole:
switch(LegacyTableColumn.at(index.column())) {
\ lots of stuff
}
}
\...
}
\...
As such, I'd prefer to avoid overriding QAbstractItemModel::roleNames()
as it will mean substantial refactoring of the above data()
function.
I have a QML view showing all the columns in each model row:
TableView {
anchors.fill: parent
model: LegacyModel
delegate: RowLayout {
implicitWidth: parent.width
Text {
text: model.display // i.e., inferred from Qt::DisplayRole
// can I access column data here somehow?
}
}
}
But I only want to display a small set of columns.
Is there a way to do that without overriding roleNames()
and using those as properties in the view?
question from:
https://stackoverflow.com/questions/65642016/qml-tableview-how-do-you-only-display-a-subset-of-model-index-data 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…