PDA

View Full Version : How to add Icons to items using QAbstractListModel in QListView



charlse
4th January 2009, 02:11
Hi All:
I don't know how to add Icons to items using QAbstractListModel in QListView ,
Is anybody know how to ?
Thanks for your suggestions.

caduel
4th January 2009, 09:55
return the icon in QAbstractItemModel::data() for role == Qt::DecorationRole

HTH

Chiz
29th December 2010, 12:36
return the icon in QAbstractItemModel::data() for role == Qt::DecorationRole

HTH

I don't understand anything you wrote.
Can you post some example code here?

AlexSudnik
29th December 2010, 13:45
Hey
You can set an icon with one of the QAbstractItem::setData overloaded methods.

Example:


item->setData(QIcon("icon.png"),Qt::DecorationRole);

javimoya
29th December 2010, 13:49
you can see many easy examples in qt assistant....
search for "model/view" topics

aamer4yu
29th December 2010, 14:31
Are you able to return text from the model ?
If yes, you must do it with Qt::DisplayRole.
Similary theres Qt::DecorationRole.. thats what caduel said

Chiz
30th December 2010, 11:19
ui.contentListView->setViewMode(QListView::IconMode);
iStandardModel = new QStandardItemModel(this);

QList<QStandardItem*> *items = new QList<QStandardItem*> ();

QString qs = ui.searchLineEdit->text();

*items = getItemsToDisplay(qs);

iStandardModel->appendColumn(*items);

//Setting the icon size
ui.contentListView->setIconSize(QSize(128, 128));
//Setting the model
ui.contentListView->setModel(iStandardModel);

In ui_GUIClass.h:


public:
QListView *contentListView;

In GUIClass.h:


public:
QStandardItemModel* iStandardModel;

The result is in attached sample.jpg.
Hope, this helps.