I have a QTreeView
and use ProxyStyle
for that.
The pic above is just the header. Now I need to draw the up/down arrow (for sorting items) beside header label as in figure. In order to put the arrow in the correct postion I need to know:
How can I calculate the text width in this case? I thought about QFontMetrics but dont know how to receive the text to calculate.
In my style I use only drawPrimitive
function
void MyStyle::drawPrimitive( PrimitiveElement p_pe, const QStyleOption *p_option, QPainter *p_painter, const QWidget *p_widget ) const
{
int leftmargin = 10;
int rightmargin = 10;
if ( p_pe == PE_IndicatorHeaderArrow )
{
if ( const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>( p_option ) )
{
QPixmap pix;
if ( header->sortIndicator & QStyleOptionHeader::SortUp )
{
pix = QPixmap( ":/sortUp.png" );
}
else if ( header->sortIndicator & QStyleOptionHeader::SortDown )
{
pix = QPixmap( ":/sortDown.png" );
}
p_painter->drawPixmap( header->rect.left() + leftmargin+ subElementRect( SE_HeaderLabel, p_option, p_widget ).width() + rightmargin, header->rect.top() + pix.height(), pix );
}
}
else
{
QProxyStyle::drawPrimitive( p_pe, p_option, p_painter, p_widget );
}
}
I use subElementRect( SE_HeaderLabel, p_option, p_widget ).width()
in this case but it is wrong. How can I calculate the width of the text?
It is all contained in the QStyleOptionHeader
. The text width could be obtained by calling:
int textWidth = header->fontMetrics.boundingRect(header->text).width();