PDA

View Full Version : Adding QPixmaps to QListView



ada10
16th August 2010, 07:57
I am adding QPixmap objects to a QLIstView in a custom widget. I am using the following code to do it -


mContentItemModel = new QStandardItemModel(this);
ui->listView->setModel(mContentItemModel);
ui->listView->clearSelection();
ui->listView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->listView->setWordWrap(true);

QStandardItem* listitem1 = new QStandardItem();
QStandardItem* listitem2 = new QStandardItem();
QStandardItem* listitem3 = new QStandardItem();

QPixmap pmap1("c:\\thumbnail1.jpg");
QVariant var1 = pmap1;
QVariant var2 = pmap1;
QVariant var3 = pmap1;
listitem1->setData( var1, Qt::DisplayRole );
listitem2->setData( var2, Qt::DisplayRole );
listitem3->setData( var3, Qt::DisplayRole );

mContentItemModel->appendRow(listitem1);
mContentItemModel->appendRow(listitem2);
mContentItemModel->appendRow(listitem3);

When I run the application , the pixmaps are not displayed in the listview. Any idea what changes are required to display the pixmaps? Should i create a custom delegate class for this ?

aamer4yu
16th August 2010, 10:01
Try using QStandardItem::setIcon
DisplayRole is meant as text.

Lykurg
16th August 2010, 10:58
...or use Qt::DecorationRole.

ada10
16th August 2010, 12:15
I tried using the setIcon API of QStandardItem API. But the image appears very small.. that is not what I want, i want the image to be bigger ( I should be able to control the size of the image). I want to have a custom widget being displayed there ... any solutions to this ?