Backport QFontMetrics::width deprecation fix

This commit is contained in:
Juergen Spitzmueller 2021-03-13 11:06:11 +01:00
parent 7922b761f5
commit 472f4f0a9d
4 changed files with 18 additions and 0 deletions

View File

@ -243,7 +243,11 @@ void CCItemDelegate::drawCategoryHeader(QPainter * painter, QStyleOptionViewItem
// draw the centered text
QFontMetrics fm(font);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
int w = fm.horizontalAdvance(category);
#else
int w = fm.width(category);
#endif
int x = opt.rect.x() + (opt.rect.width() - w) / 2;
int y = opt.rect.y() + 3 * fm.ascent() / 2;
int left = x;

View File

@ -480,7 +480,13 @@ PreambleModule::PreambleModule(QWidget * parent)
// https://stackoverflow.com/questions/13027091/how-to-override-tab-width-in-qt
const int tabStop = 4;
QFontMetrics metrics(preambleTE->currentFont());
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
// horizontalAdvance() is available starting in 5.11.0
// setTabStopDistance() is available starting in 5.10.0
preambleTE->setTabStopDistance(tabStop * metrics.horizontalAdvance(' '));
#else
preambleTE->setTabStopWidth(tabStop * metrics.width(' '));
#endif
}

View File

@ -271,7 +271,11 @@ void LayoutItemDelegate::drawCategoryHeader(QPainter * painter, QStyleOptionView
// draw the centered text
QFontMetrics fm(font);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
int w = fm.horizontalAdvance(category);
#else
int w = fm.width(category);
#endif
int x = opt.rect.x() + (opt.rect.width() - w) / 2;
int y = opt.rect.y() + fm.ascent();
int left = x;

View File

@ -640,7 +640,11 @@ QString formatToolTip(QString text, int em)
text = Qt::convertFromPlainText(text, Qt::WhiteSpaceNormal);
// Compute desired width in pixels
QFont const font = QToolTip::font();
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
int const px_width = em * QFontMetrics(font).horizontalAdvance("M");
#else
int const px_width = em * QFontMetrics(font).width("M");
#endif
// Determine the ideal width of the tooltip
QTextDocument td("");
td.setHtml(text);