Workaround for QTBUG-14949, eliding multi-line itemview items breaks

This commit is contained in:
baldurk
2018-05-03 12:35:49 +01:00
parent b8ffc1c79f
commit 644217bcbf
@@ -24,6 +24,7 @@
#include "RDTweakedNativeStyle.h"
#include <QDebug>
#include <QFontMetrics>
#include <QPainter>
#include <QPen>
#include <QStyleOption>
@@ -379,5 +380,40 @@ void RDTweakedNativeStyle::drawControl(ControlElement control, const QStyleOptio
return;
}
// https://bugreports.qt.io/browse/QTBUG-14949
// work around itemview rendering bug - the first line in a multi-line text that is elided stops
// all subsequent text from rendering. Should be fixed in 5.11, but for all other versions we need
// to manually step in. We manually elide the text before calling down to the style
#if(QT_VERSION < QT_VERSION_CHECK(5, 11, 0))
if(control == QStyle::CE_ItemViewItem)
{
const QStyleOptionViewItem *viewopt = qstyleoption_cast<const QStyleOptionViewItem *>(opt);
// only if we're eliding, not wrapping, and we have multiple lines
if((viewopt->features & QStyleOptionViewItem::WrapText) == 0 &&
viewopt->text.contains(QChar::LineSeparator))
{
const int hmargin = pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1;
QRect textRect =
subElementRect(SE_ItemViewItemText, viewopt, widget).adjusted(hmargin, 0, -hmargin, 0);
QFontMetrics metrics(viewopt->font);
QStringList lines = viewopt->text.split(QChar::LineSeparator);
for(QString &line : lines)
line = metrics.elidedText(line, viewopt->textElideMode, textRect.width(), 0);
QStyleOptionViewItem elided = *viewopt;
elided.text = lines.join(QChar::LineSeparator);
QProxyStyle::drawControl(control, &elided, p, widget);
return;
}
}
#endif
QProxyStyle::drawControl(control, opt, p, widget);
}