PDA

View Full Version : QStandardItemModel and QStandardItem



DmitryNik
8th October 2011, 10:38
Hello!

I have a question which concerns models and items in models and probably a bit QVariant. The question is: why will not appear a picture after executing the code presented bellow?



QStandardItem *item2 = new QStandardItem;
QVariant v1;
QIcon ico(":/block.png");
v1.setValue(ico);
item2->setData(v1, Qt::DisplayRole);
model->setItem(0, 0, item2);
QTreeView *tv = new QTreeView;
tv->setModel(model);
tv->show();


And another question, which was born by my curiosity, is: Does QStandardItem class's method setData work only with primitive data, such strings, integers etc.? Does it depend on QVariant-class?

llev
9th October 2011, 11:59
Try to use setIcon instead of item2->setData(v1, Qt::DisplayRole);
Does it work as expected?

DmitryNik
9th October 2011, 13:37
Thank you for answer.

Yes, it works perfectly. But still, it's a bit strange that setData() - method works only for text-data(in this case: strings, numbers etc.).

llev
9th October 2011, 13:48
Just take a look at implementation of setIcon:


inline void QStandardItem::setIcon(const QIcon &aicon)
{
setData(aicon, Qt::DecorationRole);
}

It seems the role rules

DmitryNik
9th October 2011, 16:45
Now I got it. Thank you for answering!

wysota
9th October 2011, 18:17
Does QStandardItem class's method setData work only with primitive data, such strings, integers etc.? Does it depend on QVariant-class?
It works with anything that can fit into QVariant. Since you can fit practically anything there, it works practically with anything.