[Qt-interest] QTreeView, additional images
Thomas Ehrnhoefer
thomas.ehrnhoefer at tasktop.com
Tue Apr 26 20:39:36 CEST 2011
I think I understand.. the default implementation of paint calls a
initStyleOptions and overrides whatever I set on the options with the
qmodelIndex values.
That means (I guess) if I want custom stuff being drawn, I need to
reimplement what the default paint does...
Code like that seems to work (not sure if it's good to do that though :) ):
Q_ASSERT(index.isValid());
QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
opt.text = "test";
opt.font.setBold(true);
opt.icon = imageProvider->icon(ImageProvider::MyImage);
const QWidget *widget = 0;
if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const
QStyleOptionViewItemV3 *>(&option))
{
widget = v3->widget;
}
QStyle *style = widget ? widget->style() : QApplication::style();
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
On Tue, Apr 26, 2011 at 10:47 AM, Thomas Ehrnhoefer <
thomas.ehrnhoefer at tasktop.com> wrote:
> Currently investigating custom drawing, but i am failing to make the icon
> appear.
>
> I used to create the QStandardItem with a label and an icon, and it showed
> up fine. Now I added my own QStyledItemDelegate implementation.
> First of all, if I set .text on the QStyleOptionViewItem, it does not
> override what is set on the QStandardItem (but if I invoke the empty
> constructor there, the value I set in my delegate's paint method gets used).
> This is a bit confusing as I would have thought that overriding the paint
> method takes precedence over the QStandardItem's value.
>
> Additionally (and more importantly) I am failing to draw an image. Again, I
> was hoping just to set the new icon on the option, but that draws nothing.
> Here is the code:
>
> void MyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
> option, const QModelIndex &index) const
>
> {
>
> QStyleOptionViewItemV4 optionv4(option);
>
> optionv4.text = "test";
>
> optionv4.font.setBold(true);
>
> optionv4.icon = imageProvider->icon(ImageProvider::MyImage);
>
> QStyledItemDelegate::paint(painter, optionv4, index);
>
> }
>
>
> I am trying to understand the mechanisms before going into custom drawing,
> but I seem to fail with this simple example.
>
> Thanks for any input
>
> Thomas
>
>
> On Tue, Apr 26, 2011 at 7:32 AM, Thomas Ehrnhoefer <
> thomas.ehrnhoefer at tasktop.com> wrote:
>
>> Thanks Andre, Eckhard,
>>
>> I will see what I can find out about the things you mentioned.
>> Sadly caching each of the images in composite form is less than ideal, as
>> the combinations are plenty, and at least one of the images is retrieved
>> from the network.
>>
>>
>> On Tue, Apr 26, 2011 at 2:15 AM, Eckhard Jokisch <
>> e.jokisch at orange-moon.de> wrote:
>>
>>> 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;
>>> }
>>> .....
>>> }
>>> _______________________________________________
>>> Qt-interest mailing list
>>> Qt-interest at qt.nokia.com
>>> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>>>
>>
>>
>>
>> --
>> Thomas Ehrnhoefer
>> Software Developer, http://tasktop.com
>>
>
>
>
> --
> Thomas Ehrnhoefer
> Software Developer, http://tasktop.com
>
--
Thomas Ehrnhoefer
Software Developer, http://tasktop.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110426/b6935c04/attachment.html
More information about the Qt-interest-old
mailing list