How to get all data from QStandardItem ?
-
Hey
How can I get all roles/data for given item in QStandardItem ? I want to serialize it all.Regards
DariuszFixed, example code >
i = QStandardItem("hello") i.setData("dasdas", 15241) i.setData(5325, 15242) i.setData(["vdsvds"], 15243) i.setData(["vdsvds"], 15245) ar = QByteArray() s = QDataStream(ar, QIODevice.WriteOnly) i.write(s) r = QDataStream(ar, QIODevice.ReadOnly) val = 0 v = r.readUInt32() for a in range(v): role = r.readInt32() var = r.readQVariant() print(role, var)
-
@Dariusz
It is hidden from you in private code. A vector of <role-number, value> is how it is stored and serialized. The only way you can inspect this --- and thereby get the role names used for each item --- is by doing the serialization toQDataStream
per my above and then using your own structures/code to read it back, so that you can access the elements and get the roles. See Qt source for this, e.g. https://codebrowser.dev/qt5/qtbase/src/gui/itemmodels/qstandarditemmodel_p.h.html#_ZlsR11QDataStreamRK17QStandardItemData -
@Dariusz said in How to get all data from QStandardItem ?:
there is no way to get list of roles to loop over manually?
You know which roles you're setting so you can also iterate over them.
-
@Christian-Ehrlicher Well, partially. User might set custom roles which I wont be able to track. So I'd like to query roles from item...
-
@Dariusz said in How to get all data from QStandardItem ?:
User might set custom roles
I thought you're doing the programming, not the user...
Anyway - derive from QStandardItemModel and remember the roles which are getting set.
-
@Christian-Ehrlicher Ok to summarize, there is no "getRoles" way of getting roles from QStandardItem and I have to implement it :- )
Will work on that, thanks!
-
@Dariusz
It is hidden from you in private code. A vector of <role-number, value> is how it is stored and serialized. The only way you can inspect this --- and thereby get the role names used for each item --- is by doing the serialization toQDataStream
per my above and then using your own structures/code to read it back, so that you can access the elements and get the roles. See Qt source for this, e.g. https://codebrowser.dev/qt5/qtbase/src/gui/itemmodels/qstandarditemmodel_p.h.html#_ZlsR11QDataStreamRK17QStandardItemData -
Hey @JonB
Mmm tried, I don't think its working in pyside2
i = QStandardItem("hello") i.setData("dasdas", 15241) i.setData(5325, 15242) i.setData(["vdsvds"], 15243) ar = QByteArray() s = QDataStream(ar) i.write(s) print(ar)
Got an empty buffer.
Regards
Dariusz -
Hey @JonB yep I messed up, forgot to specify stream mode
i = QStandardItem("hello") i.setData("dasdas", 15241) i.setData(5325, 15242) i.setData(["vdsvds"], 15243) i.setData(["vdsvds"], 15245) ar = QByteArray() s = QDataStream(ar, QIODevice.ReadWrite) i.write(s) r = QDataStream(ar, QIODevice.ReadOnly) val = 0 v = r.readInt32() for a in range(v): print(r.readInt32()) print(v)
I've digged in to the streaming. Looks to be a bit more of a pain to de-stream it tbh. Tricky.
Its not a list of roles
its role, data, role, data, role, data etc etc -
@JonB I'm in python, I cant just do stream >> QVector<QStandardItemData> I'm afraid :/
I'd have to deserialize each data object by hand to find correct offset byte to next role in vector I thinkUpdate!
Welp turns out there is readQVariant!i = QStandardItem("hello") i.setData("dasdas", 15241) i.setData(5325, 15242) i.setData(["vdsvds"], 15243) i.setData(["vdsvds"], 15245) ar = QByteArray() s = QDataStream(ar, QIODevice.ReadWrite) i.write(s) r = QDataStream(ar, QIODevice.ReadOnly) val = 0 v = r.readUInt32() for a in range(v): role = r.readInt32() var = r.readQVariant() print(role, var) print(v)
This works, wop wop!