Unsolved QAbstractTableModel + QML TableView: How to call setData?
-
@VRonin Ok... So now everything I got ahead of with
styleData
is no longer working. This time I am really confused. Those links threat about animations the most and what I need for now is to be able to send/ receive data from the C++ model. Is there any example for that? -
@Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:
So now everything I got ahead of with
styleData
is no longer working.What is
styleData
? -
@VRonin It was available in
itemDelegate
: https://doc.qt.io/qt-5/qml-qtquick-controls-tableview.html#itemDelegate-prop
One could use it to locate itself in the table as well as read model data. -
@Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:
One could use it to locate itself in the table as well as read model data.
now you don't need it anymore. you just access role names directly to read data and you can use
index
,row
andcolumn
directly to locate yourself within the modelBtw, this doesnt call setData:
Yes it does. If the
dataChanged
signal is not enough to convince you you can add the below toMyModel
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override{ qDebug() << "Called setData"; return QStringListModel::setData(index,value,role); }
-
@VRonin Ok, you are right! Thanks.
It feels like we are really close this time. So this is the components now:TableView { anchors.fill: parent; model: tableModel; delegate: TextInput { text: Name; onEditingFinished: { Name = text } } }
My columns and roles are still mixed together because of the obsolete behavior- this needs to be disconnected. In this case, what should the
roleNames
override method return actually? -
@Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:
hat should the roleNames override method return actually?
I would not override it at all and just use the default
edit
text: edit onEditingFinished: { edit = text }
-
@VRonin , yeah I just came to realize that too. I did:
delegate: TextInput { text: display; onEditingFinished: { edit = text } }
Which I believe in this context gives the same effect.
Alright... Thank you very much for your feedback, you helped me a lot.
Now I only need to figure out eitherheaderData
can be utilized and that would be it. -
@Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:
Now I only need to figure out either headerData can be utilized and that would be it.
I'm afraid that part has not been developed by Qt yet
We’re also working on a TableHeader, TableModel, as well as a DelegateChooser.
-
@VRonin So as for now, is there a way to set the column header at all?
-
Not exactly what I would call a header. There's this: https://doc.qt.io/qt-5/qml-qtquick-tableview.html#overlays-and-underlays but it's far from anything that is actually useful
-
@VRonin Yes, I have just checked this. It overlaps my first row... Do you know either rows inserting and removing work? Meaning- If I remove or insert a row at runtime, will it update the view?
This seems to still be a very beta thing, I wonder either old or new controls should be used. -
@Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:
I also dont know how to force the QML TreeView to call the C++ headerData method
since you use QtQuick.Controls 1 TableView you need to use the TableViewColumn element, which only supports roles.
So you can add custom role names and forward those header roles to the headerData() method in the data() method.virtual QHash<int, QByteArray> roleNames() const { QHash<int, QByteArray> rn = QAbstractItemModel::roleNames(); rn[MyHeaderRole1] = QByteArrayLiteral("header1"); return rn; } QVariantdata(const QModelIndex &index, int role = Qt::DisplayRole) const { switch( role ) { case MyHeaderRole1: return headerData(0, Qt::Horizontal); ... } return QVariant(); }
-
@raven-worx thanks for answer. The thing is that at this point I am not certain anymore which version TableView I should opt for. On one hand I dont want to use the obsolete component which has columns and roles functionality mixed, and on the other this.new one doesnt seem to be ready for usage.
-
@Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:
and on the other this.new one doesnt seem to be ready for usage.
Based on the example from the docs (untested though):
TableView { id: tableView columnWidthProvider: function (column) { var colWidth = ...; var headerItem = headerRepeater.itemAt(column) if( headerItem ) headerItem.width = colWidth; return colWidth; } topMargin: header.height Row { id: header height: 40 width: implicitWidth padding: 0 spacing: 0 Repeater { id: headerRepeater model: tableView.columns Item { height: parent.height // your column delegate } } } }
Could be easier/built-in indeed, but i think this should work (with minor adaptions probably).
QAbstractItemModel::headerData() is directly invokable from within QML
-
@Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:
On one hand I dont want to use the obsolete component which has columns and roles functionality mixed, and on the other this.new one doesnt seem to be ready for usage.
I'm not aware of a possibility to use headers directly even in the old tebleview
-
@raven-worx Thanks for answer, but I am really lost at this one. What is supposed to be my column delegate?
I am playing with this new
TableView
for over an hour now and I am not even able to set variable columns width (when the container width change). I wonder either this is a bug or a feature. -
@VRonin But would you say the new one or old one is more fit to use...?
-
@Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:
What is supposed to be my column delegate?
what every you want it to look like. A Rectangle with a Text inside maybe?
-
@raven-worx I tried a simple
Text
component and all that was visible was a single text (not sure either they were stacked one on another).Arent you guys aware any single code example showing how to do this all from ground up?
-
I understand and share your frustration on the state of views in QML, development has been focused on components that work well on mobile platforms (as QtWidgets is not really an option there) so views have been neglected.
@Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:
Do you know either rows inserting and removing work?
It does, the example I linked actually does it
I have just checked this. It overlaps my first row
looks like you forgot the
topMargin: header.implicitHeight
part