[Qt-interest] Maintaining stylesheet defs in custom delegate

Andre Somers andre at familiesomers.nl
Sun Aug 15 11:44:57 CEST 2010


  Op 14-8-2010 22:46, Neville Dastur schreef:
> 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.
First of all: next time, please create an entire new message if you ask 
a new question. You're messing up threading by replying to an existing 
one and then modifying the subject.

I am afraid that using stylesheets from your own drawing is close to 
impossible at the moment. That is quite a hole in the current 
implementation. The current Qt style sheet implementation does not 
provide any public API for accessing the relevant data for the object 
you're currently drawing. Stylesheets are only supported by the build in 
styles, and I'm afraid that also goes for any other custom drawing. At 
least, I have not found a way around it yet...

André





More information about the Qt-interest-old mailing list