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)
wrote on 11 Dec 2022, 11:34 last edited by@Dariusz
void QStandardItem::write(QDataStream &out) constWrites the item to stream out. Only the data and flags of the item are written, not the child items.
-
wrote on 11 Dec 2022, 11:52 last edited by
Mmm I was hoping to save it to json or something, there is no way to get list of roles to loop over manually?
-
Mmm I was hoping to save it to json or something, there is no way to get list of roles to loop over manually?
@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.
-
@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.
wrote on 11 Dec 2022, 12:05 last edited by@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...
-
@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.
-
@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.
wrote on 11 Dec 2022, 12:16 last edited by Dariusz 12 Nov 2022, 12:17@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!
-
Mmm I was hoping to save it to json or something, there is no way to get list of roles to loop over manually?
wrote on 11 Dec 2022, 12:19 last edited by JonB 12 Nov 2022, 12:20@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
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#_ZlsR11QDataStreamRK17QStandardItemDatawrote on 11 Dec 2022, 14:24 last edited byHey @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
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 -
@Dariusz
I don't know, but I would flush & close the data stream before I relied on whatever might be in the array.I also don't know for sure how this works when you have no model.
wrote on 11 Dec 2022, 14:32 last edited byHey @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 -
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 -
@Dariusz
But I said earlierA vector of <role-number, value> is how it is stored and serialized.
Is it not exactly that? And you can deserialize into a
QVector
.wrote on 11 Dec 2022, 14:35 last edited by Dariusz 12 Nov 2022, 14:38@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!
11/13