PDA

View Full Version : QItemDelegate and QStyle::State_Selected



cydside
25th July 2010, 18:09
Hi to all,
I'm looking for a solution to my QItemDelegate applied to a QTreeView:

ColumnIconDelegate.h


#ifndef COLUMNICONDELEGATE_H
#define COLUMNICONDELEGATE_H

#include <QtGui>

class ColumnIconDelegate: public QItemDelegate
{
public:
ColumnIconDelegate(QObject *parent = 0);

void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
};

#endif // COLUMNICONDELEGATE_H


ColumnIconDelegate.cpp


#include "ColumnIconDelegate.h"

/*----------------------------------------------------------------------------*/

ColumnIconDelegate::ColumnIconDelegate(QObject *parent)
: QItemDelegate(parent)
{
//qDebug()() << "ColumnDateDelegate Costruttore";
}

/*----------------------------------------------------------------------------*/

void ColumnIconDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QPixmap IconCalendar(48, 48);
IconCalendar.load(":sottomenu/images/calendar-empty.png", "PNG", Qt::AutoColor);

QStyleOptionViewItem myOption = option;
myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;

int pixIcona;

if (myOption.rect.height() > 48) pixIcona = 48;
else pixIcona = (myOption.rect.height());

painter->drawPixmap(myOption.rect.x(),myOption.rect.y(),pix Icona,pixIcona,IconCalendar);

drawDisplay(painter, myOption, myOption.rect, "");
drawFocus(painter, myOption, myOption.rect);
}


it shows the icon when the item is NOT selected (see picture Before_Click.png) but it disappears after the click (see picture After_Click.png),so what are I missing?
thanks

Lykurg
25th July 2010, 19:17
You have to call drawDisplay and drawFocus fist, then paint your icon. Right now you icon is covered by the later drawn blue background!

cydside
25th July 2010, 19:30
You have to call drawDisplay and drawFocus fist, then paint your icon. Right now you icon is covered by the later drawn blue background!

Ops, thanks!

aamer4yu
26th July 2010, 07:08
You can also simply override QItemDelegate::drawDecoration if you want to change only the icon.