How to make a Text wrap delegate use the treeviews stylesheet
-
Hi, I'm am trying to get a text wrapping delegate to use the stylesheet I am using for the treeview but am not sure how to do that.
The style sheet my treeview is using looks like this:
QTreeView{outline:0;} QTreeView::item:selected{background-color: #D5E9F8;color: black;} QTreeView::item{padding:5px;border: 1px solid rgb(0,0,0);} QTreeView::item:has-children{border: 1px solid rgb(0,0,0);}
My delegates paint method looks like this:
void TextWrapDelegate::paint(QPainter* painter,const QStyleOptionViewItem& option,const QModelIndex& index) const { QString text = index.model()->data(index).toString(); QTextDocument doc; doc.setDefaultFont(QFont("Times New Roman",16) ); doc.setHtml(text); doc.setTextWidth(option.rect.width() ); painter->save(); painter->translate(option.rect.x(),option.rect.y() ); doc.drawContents(painter); painter->restore(); }
The size hint function looks like this
QSize TextWrapDelegate::sizeHint(const QStyleOptionViewItem& option,const QModelIndex& index) const { QString text = index.model()->data(index).toString(); QTextDocument doc; doc.setHtml(text); doc.setTextWidth(option.rect.width() ); return QSize(doc.idealWidth()+this->v_wrapExtra,doc.size().height()+30); }
I get the text wrap I want, but it does not use the stylesheet of the treeview how can I get it to use it?
-
Hi
Store you text In EditRole and have DiaplyRole empty
Then call the QItemDelgate::paint(painter, option, index);
to have it draw its normal stuff and then
draw your text as you do now.You are not calling the normal drawing so it cant use style currently as its just your draw code.
You can see here what a normal paint does
https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qitemdelegate.cpp.html#393 -
Ok, it sorta works, but if I tried to set the DiaplyRole to empty and the EditRole to have the text it did not work.
So I had to set the text as UserRole to get that part to work.
But now the item is not high enough to contain the text when it is wrapped. How can I fix that?
-
@sailord
Hi Normally it would get that from Delegate sizeHint
but it dont respect that ?
Else you can use SizeHintRole on the model.I assume you just want to make them all a bit taller ?
there is also
tableView->verticalHeader()->setDefaultSectionSize()