AlexanderAlexander
AlexanderAlexander

Reputation: 101

Using Qt Tree Model to Store Data?

Currently:

I have created and populated a QTreeWidget with QTreeWidgetItems, and have set the text of the QTreeWidgetItems

Problem: I have no idea how to actually utilize the QTreeWidgetItems for data storage

Looking at: https://doc.qt.io/qt-5/qt.html#ItemDataRole-enum

void QTreeWidgetItem::setData(int column, int role, const QVariant &value)

I see that I can use a Qt::ItemDataRole to specify the type of data stored, but none of the options in the enumeration pertain to actually storing raw data, only Qt properties.

Questions:

Info:

Upvotes: 1

Views: 565

Answers (1)

eyllanesc
eyllanesc

Reputation: 243947

You can save the types supported by QVariant, and if it is not supported Qt points out the rules for new types to be supported, in the case of doubles it is supported so to save use the setData method to indicate and use an unused role by default like Qt::UserRole.

float data = 5.0;
int column = 0
item->setData(column, Qt::UserRole, data);

And to get the data you must convert the QVariant obtained by the data() method of the QTreeWidgetItem:

int column = 0
QVariant v = item->data(column, Qt::UserRole);
float value = v.toFloat(); // or v.value<float>();

Upvotes: 3

Related Questions