[Qt-interest] Maintaining stylesheet defs in custom delegate

Neville Dastur qt at dastur.me.uk
Sun Aug 15 20:54:30 CEST 2010


  Okay, seem to have got it working! So for anyone else looking to do 
this, the new paint() is below. It seems that drawControl() needs to 
have a valid parent widget passed to it. So HTMLDelegate is created with 
new HTMLDelegate( [pointer to treeView Object] );

void HTMLDelegate::paint(QPainter *painter, const QStyleOptionViewItem 
&option, const QModelIndex &index) const
{
     Q_ASSERT(index.isValid());

     QStyleOptionViewItemV4 optionV4 = option;
     initStyleOption(&optionV4, index);

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

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

     // Paint item without text
     optionV4.text = QString();
     style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter, 
qobject_cast<QWidget *>(this->parent()));
     
//                                                                                                   
^^^^^^ This seem to be the important bit
     QAbstractTextDocumentLayout::PaintContext ctx;

     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();
}

--

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




More information about the Qt-interest-old mailing list