PDA

View Full Version : adding one icon to all items in QTreeView



Mystical Groovy
5th October 2009, 15:11
hey all,
i would like to add one icon to all items in qtreeview

i tried the following:
(listModel is my QDirModel)


int role;
QIcon modelicon("/images/package.png");
modelicon.operator QVariant();
listModel->setData(listModel->index("/home/test/"),modelicon, role = Qt::DecorationRole);


int role;
listModel->setData(listModel->index("/home/test/"),QVariant(QIcon("/images/package.png")), role = Qt::DecorationRole);


listModel->setData(listModel->index("/home/test/"),QVariant(QIcon("/images/package.png")), Qt::DecorationRole);

the program compiles and runs successfully but it doesnt add my icon in the items in QTreeView, it continues to show the default icon.

caduel
5th October 2009, 16:33
i) why do you assign the role to your variable role?
(Looks like you are confused about default values...)

ii) check the return value of setData(); probably it is false.
Not every model supports setting data (or setting any role).


You can subclass QDirModel and have your subclass's data function return that icon for Qt::DecorationRole.

HTH

Mystical Groovy
5th October 2009, 18:05
thanks for your reply :)

youre right im deeply confused im not used to the "model" system yet haha.
can you help me with some example code on how to do this?

thank you

aamer4yu
6th October 2009, 10:47
MyModel::data ( const QModelIndex & index, int role = Qt::DisplayRole )
{
if(role == Qt::DecorationRole)
return QIcon(...);

// Rest of the code..
}

Mystical Groovy
6th October 2009, 11:10
thank you, but the problem remains,
this is the code:


int role;
listModel->data ( listModel->index("/home/test"), role = Qt::DisplayRole );
if(role == Qt::DecorationRole)
{
return QIcon("");
}


it gives me
" error: return-statement with a value, in function returning 'void'"

Mystical Groovy
6th October 2009, 12:09
i got it working after all,
problem solved

aamer4yu
6th October 2009, 13:13
Can you share what was the mistake ? It might help others :)