Unsolved QAbstractTableModel + QML TableView: How to call setData?
-
So my role names would be Name, Unit and Value. How would this be consistent with the edit way if its custom?
easy, replace
edit
withName
,Unit
orValue
. For example, if theName
role will contain aQString
you can use:delegate: TextInput { text: Name onEditingFinished: { Name = text } }
My override looks as follows:
This is a very convoluted way to go about it. You can just use
QMetaEnum::fromType<Derived::Columns>().valueToKeys(col);
for (int col = 0; col < colCount; col++)
Looks like columns and roles are the same thing here but since 1 year ago they are now 2 distinct things (as they are in QtWidgets): https://blog.qt.io/blog/2018/08/29/tableview/
-
@VRonin Thank you for follow-up. As for the method optimization, I will get back to that. But for the main problem, I did:
Component { id: theInputComp; TextInput { id: textInputId; anchors.fill: parent; onEditingFinished: { theLoader.visible = false; theCellText.visible = true; //tableModel.setData(styleData.row, //styleData.column, text); Name = text; } } }
And received:
qrc:/UBasicTable.qml:109: Error: Invalid write to global property "Name"
Am I missing something? -
Name
might be a reserved word. Try changingName = 0,
tocolName = 0,
and usetext: colName
andcolName = text
-
@VRonin Doesnt matter how I call it:
qrc:/UBasicTable.qml:109: Error: Invalid write to global property "colName" qrc:/UBasicTable.qml:109: Error: Invalid write to global property "colName1" // this doesnt exist
Any other hints :/?
-
Just BTW... Figured out why doesnt the
dataChanged
signal fire the table update. I was calling it like this:emit dataChanged(index, index, {role});
and
role
was alwaysQt::EditRole
. Now I call it without roles parameter:emit dataChanged(index, index);
And the TableView cells are being updated! Thanks for the help @VRonin, it led me to the right place for this one.
I still havent figured out how to automatically callsetData
from the QML though (using the presented workaround). If you have any more ideas please let me know. -
Are you sure you are inside
delegate:
?
This is a minimal working example:#include <QGuiApplication> #include <QQmlContext> #include <QQmlApplicationEngine> #include <QStringListModel> #include <QDebug> class MyModel : public QStringListModel{ public: MyModel(QObject *parent = nullptr) : QStringListModel(parent) {} QHash<int, QByteArray> roleNames() const override { QHash<int, QByteArray> roles; roles[Qt::EditRole] = QByteArrayLiteral("TestRole"); roles[Qt::DisplayRole] = QByteArrayLiteral("AnotherRole"); return roles; } }; int main(int argc, char *argv[]) { #if defined(Q_OS_WIN) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); QQmlApplicationEngine engine; MyModel model; QObject::connect(&model,&MyModel::dataChanged,[](const QModelIndex& idx){ qDebug() << "Changed: " << idx.row() << " " << idx.data().toString(); }); model.setStringList(QStringList{"aaa","bbb","ccc"}); engine.rootContext()->setContextProperty("mymodel", &model); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
import QtQml 2.2 import QtQuick 2.7 import QtQuick.Window 2.2 import QtQuick.Controls 2.3 Window { visible: true width: 640 height: 480 title: qsTr("Test Edit") ListView { anchors.fill: parent; width: 200; height: 250 model: mymodel delegate: TextInput { text: TestRole onEditingFinished: { TestRole = text } } } }
-
@VRonin Hmmm... Here is the thing- I am inside
itemDelegate
notdelegate
.delegate
is available in differentQtQuick.Controls
version. If I switch to it, I will no longer have my other components available, iePopup
. IsntitemDelegate
anddelegate
the same thing?Look at the docs,
TableView
has nodelegate
member: https://doc.qt.io/qt-5/qml-qtquick-controls-tableview.html#rowDelegate-prop -
Looks like you are still using Qt Quick controls 1.
Qt Quick controls 2 is already 3 years old.If I switch to it, I will no longer have my other components available, ie Popup
Qt Quick Controls 2 has https://doc.qt.io/qt-5/qml-qtquick-controls2-popup.html
-
@VRonin
QtQuick.Controls 2.5
do havedelegate
but dont haveTableViewColumn
which I am using for columns setting. Is there a better way now? -
@Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:
Is there a better way now?
@VRonin said in QAbstractTableModel + QML TableView: How to call setData?:
Looks like columns and roles are the same thing here but since 1 year ago they are now 2 distinct things (as they are in QtWidgets): https://blog.qt.io/blog/2018/08/29/tableview/
-
@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