PDA

View Full Version : How to add image in QListWidget



mekos
4th August 2008, 18:17
Hello

I have some lines passed in a QListWidget using listWidget->addItem ( dog ),listWidget->addItem ( cat ) etc..

Can someone tell me how can i add a picture near the text??
Thanks!

spirit
4th August 2008, 18:19
see the QTDIR\examples\itemviews\stardelegate\ example.

spirit
4th August 2008, 18:21
or QTDIR\demos\interview\

mekos
4th August 2008, 18:29
not helpful..i have the pictures already in the resources.qrc.Just need a method to display them in the listwidget as items..any hint?

spirit
4th August 2008, 18:43
QVariant Model::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
static QIcon services(QPixmap(":/images/yourimage.png"));
if (role == Qt::DecorationRole)
return qVariantFromValue(services);
return QVariant();
}

mekos
4th August 2008, 18:52
there is no need to pass the images through index..
the code is like


...
QListWidgetItem* dog = new QListWidgetItem ( tr ( ""its a dog ) );
QListWidgetItem* cat = new QListWidgetItem ( tr ( "its a cat" ) );
QListWidgetItem* lion = new QListWidgetItem ( tr ( "its a lion" ) );
//just need to construct 3 image items here
list->addItem(dog);
list->addItem(cat);
list->addItem(lion);
//and pass them here for every animal

spirit
4th August 2008, 19:02
maybe this code will help


QListWidget *lw = new QListWidget(this);
QListWidgetItem *itm = new QListWidgetItem(tr("one"));
itm->setIcon(QIcon(":/images/image.png"));
lw->addItem(itm);

mekos
4th August 2008, 19:12
You're very helful today my friend :) thanks!
Do you know how to resize a specified column in a treeview?

spirit
4th August 2008, 19:19
read more about this method


void QHeaderView::setResizeMode ( int logicalIndex, ResizeMode mode )


and


void QHeaderView::resizeSection ( int logicalIndex, int size )