[Qt-interest] Maintaining stylesheet defs in custom delegate

Neville Dastur qt at dastur.me.uk
Sun Aug 15 14:10:26 CEST 2010


On 15/08/2010 10:44, Andre Somers wrote:
>    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.
>
>    
Sorry, I realised just as I pressed send, what would happen!
> 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...
>    
I see. The Qt blg post 
http://labs.trolltech.com/blogs/2007/11/01/qstyleditemdelegate-styling-item-views/
inferred that by using QStyledItemDelegate this issue would be sorted out.

 From your statement, is it a waste of time delving into the Qt library 
source? Because, presumably Qt must manage it somehow.

Neville
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100815/196da585/attachment.html 


More information about the Qt-interest-old mailing list