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 // draw the centered text
QFontMetrics fm(font); QFontMetrics fm(font);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
int w = fm.horizontalAdvance(category);
#else
int w = fm.width(category); int w = fm.width(category);
#endif
int x = opt.rect.x() + (opt.rect.width() - w) / 2; int x = opt.rect.x() + (opt.rect.width() - w) / 2;
int y = opt.rect.y() + 3 * fm.ascent() / 2; int y = opt.rect.y() + 3 * fm.ascent() / 2;
int left = x; 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 // https://stackoverflow.com/questions/13027091/how-to-override-tab-width-in-qt
const int tabStop = 4; const int tabStop = 4;
QFontMetrics metrics(preambleTE->currentFont()); 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(' ')); preambleTE->setTabStopWidth(tabStop * metrics.width(' '));
#endif
} }

View File

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

View File

@ -640,7 +640,11 @@ QString formatToolTip(QString text, int em)
text = Qt::convertFromPlainText(text, Qt::WhiteSpaceNormal); text = Qt::convertFromPlainText(text, Qt::WhiteSpaceNormal);
// Compute desired width in pixels // Compute desired width in pixels
QFont const font = QToolTip::font(); 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"); int const px_width = em * QFontMetrics(font).width("M");
#endif
// Determine the ideal width of the tooltip // Determine the ideal width of the tooltip
QTextDocument td(""); QTextDocument td("");
td.setHtml(text); td.setHtml(text);