[Qt-interest] decoration role for the tree model subclass

Sebastian Fischer creat at gmx.net
Wed Dec 17 15:45:00 CET 2008


You never said what view class you were using, but I assume it's a
QTreeView since you've implemented a tree model?

QTreeView should (as all of Qts own views) show the icon by requesting
the decoration role. If you're not sure just output the requested role
to  the debug console or something so you're sure it gets requested (I
think  Qt::DecorationRole  is  defined as 1, so if data is called with
role == 1 everything is fine).

If it doesn't work it's most likely a problem with the icon, not with
it being requested.

[Sorry my previous mail didn't show up on the mailing list, I had some
problems so I sent my reply directly to him]

Bye,
Sebastian Fischer

on 17.12.2008 at 14:08 you wrote:
> Hello Sebastian,

> I did according to your hint. 

> But the icon is not showing up along with  the item name.

> I think the reason is that data() function is called with display role by default.

> How to call  that with  the decoration role?

> I would to understand the function call hierarchy in the model that i have created

> The manual says that for creating a tree model we have to subclass the QAbstractItemModel and must
> reimplement some of the functions. I would like to see how these over-ridden functions are called.

> Only then i would probably be able to track and call the data() with the decoration role.


> What do you think?


> Regards
> Sajjad

> ---------- Forwarded message ----------
> From: Sebastian Fischer <creat at gmx.net>
> Date: Wed, Dec 17, 2008 at 6:37 AM
> Subject: Re: [Qt-interest] Fwd: decoration role for the tree model subclass
> To: ami guru <dosto.walla at gmail.com>


> Hi,

> below is your data() function adapted to correctly handle the decoration role.
> This funtion serves as the source of all information about the item
> referenced to by the given index. You already handle the
> Qt::DisplayRole and just have to expand it for the
> Qt::DecorationRole, since the type of data requested from the view
> is defined in the role variable, like so:

> <<< BEGIN CODE >>>
> QVariant H3DTreeModel::data(const QModelIndex &index, int role) const
> { 
>        if (!index.isValid())
>                return QVariant();
>        

>        H3DTreeItem *item =
> static_cast<H3DTreeItem*>(index.internalPointer());

>        if (role == Qt::DisplayRole)
>                return item->data(index.column());
>        else if (role == Qt::DecorationRole)
>                return item->icon();
>        else
>                return QVariant();        
> }
> <<< END CODE >>>

> Obviously H3DTreeItem need a (new) member function returning a
> QIcon (or just can get the icon from somewhere else).

> You might want to read through this, the 'Item Roles' section in particular:
> http://doc.trolltech.com/4.4/model-view-model.html

> Bye,
> Sebastian Fischer


> on 17.12.2008 at 01:19 you wrote:

> hello ,


> do i have to overload the data() function for Qt::DecorationRole ?

> The link you provided did not make much sense to me

> Could you please provide more reference to that?


> Regards
> Sajjad


> ---------- Forwarded message ----------
> From: Benjamin <ikipou at gmail.com>
> Date: Wed, Dec 17, 2008 at 12:18 AM
> Subject: Re: [Qt-interest] decoration role for the tree model subclass
> To: Qt Interest List <qt-interest at trolltech.com>


> 2008/12/16 ami guru <dosto.walla at gmail.com>

> How to generate the icon in the above code and assign role for that

> Hi, 

> The documentation says that the role for icons is
> Qt::DecorationRole:
> http://doc.trolltech.com/4.4/qt.html#ItemDataRole-enum

> best regards,
> Benjamin

> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest













More information about the Qt-interest-old mailing list