[LyX/2.3.x] Backport QFontMetrics::width deprecation fix

Juergen Spitzmueller spitz at lyx.org
Sat Mar 13 10:00:57 UTC 2021


commit 472f4f0a9d094ace7e0964f8ba7e2f1be6ac0ace
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Sat Mar 13 11:06:11 2021 +0100

    Backport QFontMetrics::width deprecation fix
---
 src/frontends/qt4/CategorizedCombo.cpp |    4 ++++
 src/frontends/qt4/GuiDocument.cpp      |    6 ++++++
 src/frontends/qt4/LayoutBox.cpp        |    4 ++++
 src/frontends/qt4/qt_helpers.cpp       |    4 ++++
 4 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/src/frontends/qt4/CategorizedCombo.cpp b/src/frontends/qt4/CategorizedCombo.cpp
index 7571963..b585a17 100644
--- a/src/frontends/qt4/CategorizedCombo.cpp
+++ b/src/frontends/qt4/CategorizedCombo.cpp
@@ -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;
diff --git a/src/frontends/qt4/GuiDocument.cpp b/src/frontends/qt4/GuiDocument.cpp
index 544e52d..41b08fc 100644
--- a/src/frontends/qt4/GuiDocument.cpp
+++ b/src/frontends/qt4/GuiDocument.cpp
@@ -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
 }
 
 
diff --git a/src/frontends/qt4/LayoutBox.cpp b/src/frontends/qt4/LayoutBox.cpp
index 5ed7f2f..67d0541 100644
--- a/src/frontends/qt4/LayoutBox.cpp
+++ b/src/frontends/qt4/LayoutBox.cpp
@@ -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;
diff --git a/src/frontends/qt4/qt_helpers.cpp b/src/frontends/qt4/qt_helpers.cpp
index d23ae9c..b5cd928 100644
--- a/src/frontends/qt4/qt_helpers.cpp
+++ b/src/frontends/qt4/qt_helpers.cpp
@@ -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);


More information about the lyx-cvs mailing list