[Qt-interest] Maintaining stylesheet defs in custom delegate

Neville Dastur qt at dastur.me.uk
Sat Aug 14 22:46:28 CEST 2010


Hi,

I am having trouble keeping my stylesheet definitions applied to my 
custom delegates items

So in my stylesheet I have:
QTreeView::item:hover {
     background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 
#e7effd, stop: 1 #cbdaf1);
     border: 1px solid #bfcde4;
     }

QTreeView::item:selected {
     border: 1px solid #567dbc;
     }

and my delegate code to render HTML is:

HTMLDelegate::HTMLDelegate(QObject *parent/*=0*/) :
         QStyledItemDelegate(parent)
{
}

void HTMLDelegate::paint(QPainter *painter, const QStyleOptionViewItem 
&option, const QModelIndex &index) const
{
     QStyleOptionViewItemV4 optionV4 = option;
     initStyleOption(&optionV4, index);

     QStyle *style = optionV4.widget? optionV4.widget->style() : 
QApplication::style();

     QTextDocument doc;
     doc.setHtml(optionV4.text);

     // ******* if this is commented out then no highlighting at all 
occurs *******
     if(optionV4.state & QStyle::State_MouseOver)
         optionV4.backgroundBrush = option.palette.color( 
QPalette::Highlight );

     // Paint item without text
     optionV4.text = QString();
     style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter);

     QAbstractTextDocumentLayout::PaintContext ctx;

     // Highlighting text if item is selected
     if (optionV4.state & QStyle::State_Selected)
         ctx.palette.setColor(QPalette::Text, 
optionV4.palette.color(QPalette::Active, QPalette::HighlightedText));

     QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, 
&optionV4);
     painter->save();
     painter->translate(textRect.topLeft());
     painter->setClipRect(textRect.translated(-textRect.topLeft()));
     doc.documentLayout()->draw(painter, ctx);
     painter->restore();
}

I am sure I am missing something obvious, but can't find it in any 
documentation. Actually, I suspect that getting the highlight color from 
QPalette is "wrong", but where is the highlight color defined for the 
currently active stylesheet?

Thank you in advance for any pointers.

Neville


-- 
Surgeons Net Education
w: http://www.surgeons.org.uk
e: info at surgeons.org.uk




More information about the Qt-interest-old mailing list