QHeaderView: respect the font role while calculating the elided text
The font role in the header view was not taken into account when the text for an item should be elided. This leads to a wrongly elided text esp. visible when the font size is different to the font of QHeaderView. Fix it by passing the elide mode to the style since only the style knows the used font (e.g. bold or not bold) and available rect. This is now in sync with CE_ItemViewItem where the eliding is also done by the style and not by the item view. [ChangeLog][QtWidgets][QHeaderView] QStyleOptionHeader got a new member textElideMode. Fixes: QTBUG-86426 Change-Id: If6914fe5aaa5d285e6da55d2129f9249d90da3d7 Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
parent
1051e23ce9
commit
4d94384612
@ -1,3 +1,5 @@
|
||||
|
||||
|
||||
#####################################################################
|
||||
# Main projectfile
|
||||
#####################################################################
|
||||
|
@ -2952,10 +2952,11 @@ void QHeaderView::initStyleOptionForIndex(QStyleOptionHeader *option, int logica
|
||||
margin += style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this) +
|
||||
style()->pixelMetric(QStyle::PM_HeaderMargin, nullptr, this);
|
||||
|
||||
if (d->textElideMode != Qt::ElideNone) {
|
||||
const QRect textRect = style()->subElementRect(QStyle::SE_HeaderLabel, &opt, this);
|
||||
opt.text = opt.fontMetrics.elidedText(opt.text, d->textElideMode, textRect.width() - margin);
|
||||
}
|
||||
QVariant var = d->model->headerData(logicalIndex, d->orientation,
|
||||
Qt::FontRole);
|
||||
if (var.isValid() && var.canConvert<QFont>())
|
||||
opt.fontMetrics = QFontMetrics(qvariant_cast<QFont>(var));
|
||||
opt.textElideMode = d->textElideMode;
|
||||
|
||||
QVariant foregroundBrush = d->model->headerData(logicalIndex, d->orientation,
|
||||
Qt::ForegroundRole);
|
||||
|
@ -1659,13 +1659,20 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt,
|
||||
else
|
||||
rect.setRight(rect.right() - pixw - margin);
|
||||
}
|
||||
QFontMetrics fm(header->fontMetrics);
|
||||
if (header->state & QStyle::State_On) {
|
||||
QFont fnt = p->font();
|
||||
fnt.setBold(true);
|
||||
p->setFont(fnt);
|
||||
fm = QFontMetrics((p->font()));
|
||||
}
|
||||
QString text;
|
||||
if (header->textElideMode != Qt::ElideNone)
|
||||
text = fm.elidedText(header->text, header->textElideMode, rect.width());
|
||||
else
|
||||
text = header->text;
|
||||
proxy()->drawItemText(p, rect, header->textAlignment, header->palette,
|
||||
(header->state & State_Enabled), header->text, QPalette::ButtonText);
|
||||
header->state.testFlag(State_Enabled), text, QPalette::ButtonText);
|
||||
}
|
||||
break;
|
||||
#if QT_CONFIG(toolbutton)
|
||||
|
@ -739,11 +739,7 @@ QStyleOptionGroupBox::QStyleOptionGroupBox(int version)
|
||||
*/
|
||||
|
||||
QStyleOptionHeader::QStyleOptionHeader()
|
||||
: QStyleOption(QStyleOptionHeader::Version, SO_Header),
|
||||
section(0), textAlignment(Qt::AlignLeft), iconAlignment(Qt::AlignLeft),
|
||||
position(QStyleOptionHeader::Beginning),
|
||||
selectedPosition(QStyleOptionHeader::NotAdjacent), sortIndicator(None),
|
||||
orientation(Qt::Horizontal)
|
||||
: QStyleOptionHeader(QStyleOptionHeader::Version)
|
||||
{
|
||||
}
|
||||
|
||||
@ -755,7 +751,7 @@ QStyleOptionHeader::QStyleOptionHeader(int version)
|
||||
section(0), textAlignment(Qt::AlignLeft), iconAlignment(Qt::AlignLeft),
|
||||
position(QStyleOptionHeader::Beginning),
|
||||
selectedPosition(QStyleOptionHeader::NotAdjacent), sortIndicator(None),
|
||||
orientation(Qt::Horizontal)
|
||||
orientation(Qt::Horizontal), textElideMode(Qt::ElideNone), unused(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -219,7 +219,9 @@ public:
|
||||
SectionPosition position;
|
||||
SelectedPosition selectedPosition;
|
||||
SortIndicator sortIndicator;
|
||||
Qt::Orientation orientation;
|
||||
Qt::Orientation orientation:2;
|
||||
Qt::TextElideMode textElideMode:2;
|
||||
int unused:28;
|
||||
|
||||
QStyleOptionHeader();
|
||||
QStyleOptionHeader(const QStyleOptionHeader &other) : QStyleOption(Version, Type) { *this = other; }
|
||||
|
Loading…
x
Reference in New Issue
Block a user