No Icons in TreeView
-
Hi everybody
I'm trying to make a my own model to be used in a treeview and i'd like to have icons on my items.
in my model i've implemented the data function as@
QVariant PipeDesignItemModel::data(const QModelIndex &index, int role) const
{if (!index.isValid()) return QVariant(); if (role != Qt::DisplayRole) return QVariant(); QObject * Item ; Item = static_cast<QObject*>(index.internalPointer()); QVariant retObj; QString iconfile = "c:/GreenPipe.ico"; QIcon icon = QIcon(iconfile); qDebug() << icon.isNull() ; if (role == Qt::DecorationRole) retObj.setValue( QIcon(iconfile)); retObj.setValue(Item->objectName()); return retObj;}
@However the role is never Qt::DecorationRole so the icon is never shown. What do i have to do to change the role?
-
This role never reaches your if:
@
QVariant PipeDesignItemModel::data(const QModelIndex &index, int role) const
{if (!index.isValid()) return QVariant(); if (role != Qt::DisplayRole) // here you return if item role is decoration :-) return QVariant(); // .... if (role == Qt::DecorationRole) retObj.setValue( QIcon(iconfile)); retObj.setValue(Item->objectName()); return retObj;}
@ -
I fixed it and the icon now shows. But not the text.
@
QVariant PipeDesignItemModel::data(const QModelIndex &index, int role) const
{if (!index.isValid()) return QVariant(); if (index.column()==1 && role != Qt::DisplayRole) return QVariant(); if (index.column()==0 && role != Qt::DecorationRole) return QVariant(); QObject * Item ; Item = static_cast<QObject*>(index.internalPointer()); QVariant retObj; retObj.setValue(Item->objectName()); if (role == Qt::DecorationRole) retObj.setValue(greenPipe); return retObj;}
@Can i show an icon and text at the same time?
-
you are showing them in different columns.
what is your view?
typically, the roles are used in the same column:@
QVariant PipeDesignItemModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();QObject* Item = static_cast<QObject*>(index.internalPointer()); QVariant retObj; switch(role) { case Qt::DisplayRole: retObj.setValue(Item->objectName()); break; case Qt::DecorationRole: retObj.setValue(greenPipe); break; } return retObj;}@
this would show icon and text in each column. if you want to seperate by column, do it aditionally...
-
Can somebody tell me how to resize the icon? I don't get it...