[Qt-interest] QTreeView, additional images

Eckhard Jokisch e.jokisch at orange-moon.de
Tue Apr 26 11:15:02 CEST 2011


On Tuesday 26 April 2011 01:27:54 Thomas Ehrnhoefer wrote:
> Hi
> 
> I have a TreeViewer, and I would like to have additional images available.
> I don't want multiple columns, just additional decorations (images) to
> (some) of the items (e.g. a "changed" indicator). An example would be
> http://oboedit.org/docs/images/tree_view_single.png
> 
> Since I am new to Qt, I am wondering what the best way (if there is any) to
> do this would be. Most likely some custom drawing I assume, but I am unsure
> on how/where to start.
> 
> Any hints are very welcome
> 
> Thanks
> Thomas

I think what you are looking for is something like the below implemented into 
your ::data() function of the model where "item" is an item out of the model 
and the item->status() returns either CHANGED or UNCHANGED or somethins else 
useful.
The switch over the actual index.column() allows you to have different icons in 
different collumns - which you don't need right now but you never know what's 
coming next ;)

Cheers
Eckhard
.....
QVariant YourModel::::data(const QModelIndex &index, int role) const
 {
     if (!index.isValid())
         return QVariant();

	if (role == Qt::DecorationRole )
       {
         QPixmap p;

         switch(index.column())
         {
         case 0:
             switch(item->status() )
             {
             case CHANGED:
                 p.load(":/icons/changed-icon.png");
                 break;
             case UNCHANGED:
                 p.load(":/icons/unchanged-icon.png");
                 break;
             default:
                 p.load(":/icons/some-icon.png");
                 break;
             }
             break;
         case 1:
             if(p.isNull())
             {
                 p.load(":/icons/warn.png");
             }
             else
             {
                 p.load(":/icons/ok.png");
             }
             break;
         default:
             break;
         }
         return p;
       }
.....
}



More information about the Qt-interest-old mailing list