On Fri, Feb 28, 2020 at 02:35:18PM -0500, Scott Kostyshak wrote:
> Compiling LyX with Qt 5.14.1 gives deprecation warnings, which breaks
> compilation of LyX with -Werror. I'm working on a patch to address
> these. I'm sending this message just to avoid duplication of effort in
> case anyone else comes across these warnings. I'll hopefully finish the
> patch next week.

In Qt 5.11, horizontalAdvance() was added "to replace the confusingly
named width() function", which was deprecated in the same release. [1]

In the compilation warning message, Qt says to use horizontalAdvance()
instead of width() but one webpage [2] suggests to be more careful:

  "I am pretty sure that in most cases QFontMetrics::boundingRect() is
  what you want, unless you are writing custom text shaping/layouting
  code".

From what I understand, we are indeed writing custom text
shaping/layouting code.

Attached is a patch. I don't propose to commit this patch; I just attach
it to give an idea of how many replacements are needed and in case
someone can check that we do indeed want horizontalAdvance() as the
replacement in all cases and not boundingRect().

JMarc, I'm guessing you're the one that I should bother about taking a
look at this specific change. Can you take a look when you have time? If
you prefer to not think about this or don't have time, it can wait; it's
just a compilation warning message.

After figuring out the above, the next question would be how to
implement the changes in a way that preserves compatibility with Qt
versions < 5.11. Should we use a directive to condition on the Qt
version around each instance or since there are several instances should
we use the approach of a macro as is done at [3]?

Note that the changes in this patch are just the ones I needed in order
to avoid the warning when building LyX. It's possible there are other
uses of width() that I did not need to compile (e.g., because of ifdef
directives).

I had said (in the first message of this thread) that I hoped to address
all of the Qt 5.14 deprecation warnings by next week. However, I don't
think I will finish by then. There are a couple of warnings that I need
to study in order to figure out a proposed change.

Scott


[1] https://code.qt.io/cgit/qt/qtbase.git/tree/dist/changes-5.11.0/?h=v5.11.0

[2] 
https://kdepepo.wordpress.com/2019/08/05/about-deprecation-of-qfontmetricswidth/

[3] https://phabricator.kde.org/D22434
diff --git a/src/frontends/qt/CategorizedCombo.cpp b/src/frontends/qt/CategorizedCombo.cpp
index 4acae5bc34..9971a1c580 100644
--- a/src/frontends/qt/CategorizedCombo.cpp
+++ b/src/frontends/qt/CategorizedCombo.cpp
@@ -244,7 +244,7 @@ void CCItemDelegate::drawCategoryHeader(QPainter * painter, QStyleOptionViewItem
 
 	// draw the centered text
 	QFontMetrics fm(font);
-	int w = fm.width(category);
+	int w = fm.horizontalAdvance(category);
 	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/qt/GuiDocument.cpp b/src/frontends/qt/GuiDocument.cpp
index 2d1cc7628f..8ea75eacaf 100644
--- a/src/frontends/qt/GuiDocument.cpp
+++ b/src/frontends/qt/GuiDocument.cpp
@@ -495,7 +495,7 @@ PreambleModule::PreambleModule(QWidget * parent)
 	// https://stackoverflow.com/questions/13027091/how-to-override-tab-width-in-qt
 	const int tabStop = 4;
 	QFontMetrics metrics(preambleTE->currentFont());
-	preambleTE->setTabStopWidth(tabStop * metrics.width(' '));
+	preambleTE->setTabStopWidth(tabStop * metrics.horizontalAdvance(' '));
 }
 
 
diff --git a/src/frontends/qt/GuiFontExample.cpp b/src/frontends/qt/GuiFontExample.cpp
index cb27b5fb5a..881ed6c2d8 100644
--- a/src/frontends/qt/GuiFontExample.cpp
+++ b/src/frontends/qt/GuiFontExample.cpp
@@ -29,7 +29,7 @@ void GuiFontExample::set(QFont const & font, QString const & text)
 QSize GuiFontExample::sizeHint() const
 {
 	QFontMetrics m(font_);
-	return QSize(m.width(text_) + 10, m.ascent() + m.descent() + 6);
+	return QSize(m.horizontalAdvance(text_) + 10, m.ascent() + m.descent() + 6);
 }
 
 
diff --git a/src/frontends/qt/GuiFontMetrics.cpp b/src/frontends/qt/GuiFontMetrics.cpp
index acc804460d..20cceddab3 100644
--- a/src/frontends/qt/GuiFontMetrics.cpp
+++ b/src/frontends/qt/GuiFontMetrics.cpp
@@ -237,7 +237,7 @@ int GuiFontMetrics::width(docstring const & s) const
 	bool const math_char = s.length() == 1;
 #endif
 	// keep value 0 for math chars with width 0
-	if (!math_char || metrics_.width(toqstr(s)) != 0) {
+	if (!math_char || metrics_.horizontalAdvance(toqstr(s)) != 0) {
 		QTextLayout tl;
 		tl.setText(toqstr(s));
 		tl.setFont(font_);
@@ -573,9 +573,9 @@ int GuiFontMetrics::width(char_type c) const
 		return value;
 
 	if (is_utf16(c))
-		value = metrics_.width(ucs4_to_qchar(c));
+		value = metrics_.horizontalAdvance(ucs4_to_qchar(c));
 	else
-		value = metrics_.width(toqstr(docstring(1, c)));
+		value = metrics_.horizontalAdvance(toqstr(docstring(1, c)));
 
 	width_cache_.insert(c, value);
 
diff --git a/src/frontends/qt/GuiView.cpp b/src/frontends/qt/GuiView.cpp
index 1b98d989e1..d5f60e62c2 100644
--- a/src/frontends/qt/GuiView.cpp
+++ b/src/frontends/qt/GuiView.cpp
@@ -217,8 +217,8 @@ public:
 		int hline = fm.height();
 		QStringList::const_iterator sit;
 		for (sit = titlesegs.constBegin(); sit != titlesegs.constEnd(); ++sit) {
-			if (fm.width(*sit) > wline)
-				wline = fm.width(*sit);
+			if (fm.horizontalAdvance(*sit) > wline)
+				wline = fm.horizontalAdvance(*sit);
 		}
 		// The longest line in the reference font (for English)
 		// is 180. Calculate scale factor from that.
diff --git a/src/frontends/qt/LayoutBox.cpp b/src/frontends/qt/LayoutBox.cpp
index 7167eba3dc..9c90900d94 100644
--- a/src/frontends/qt/LayoutBox.cpp
+++ b/src/frontends/qt/LayoutBox.cpp
@@ -275,7 +275,7 @@ void LayoutItemDelegate::drawCategoryHeader(QPainter * painter, QStyleOptionView
 
 	// draw the centered text
 	QFontMetrics fm(font);
-	int w = fm.width(category);
+	int w = fm.horizontalAdvance(category);
 	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/qt/PanelStack.cpp b/src/frontends/qt/PanelStack.cpp
index c61c8e0244..92dff4434a 100644
--- a/src/frontends/qt/PanelStack.cpp
+++ b/src/frontends/qt/PanelStack.cpp
@@ -121,7 +121,7 @@ void PanelStack::addCategory(QString const & name, QString const & parent)
 	QFontMetrics fm(list_->font());
 
 	// calculate the real size the current item needs in the listview
-	int itemsize = fm.width(qt_(name)) + 10 + list_->indentation() * depth;
+	int itemsize = fm.horizontalAdvance(qt_(name)) + 10 + list_->indentation() * depth;
 	// adjust the listview width to the max. itemsize
 	if (itemsize > list_->minimumWidth())
 		list_->setMinimumWidth(itemsize);
diff --git a/src/frontends/qt/qt_helpers.cpp b/src/frontends/qt/qt_helpers.cpp
index 0311b8b25f..3a436aa844 100644
--- a/src/frontends/qt/qt_helpers.cpp
+++ b/src/frontends/qt/qt_helpers.cpp
@@ -640,7 +640,7 @@ QString formatToolTip(QString text, int em)
 		text = Qt::convertFromPlainText(text, Qt::WhiteSpaceNormal);
 	// Compute desired width in pixels
 	QFont const font = QToolTip::font();
-	int const px_width = em * QFontMetrics(font).width("M");
+	int const px_width = em * QFontMetrics(font).horizontalAdvance("M");
 	// Determine the ideal width of the tooltip
 	QTextDocument td("");
 	td.setHtml(text);

Attachment: signature.asc
Description: PGP signature

-- 
lyx-devel mailing list
lyx-devel@lists.lyx.org
http://lists.lyx.org/mailman/listinfo/lyx-devel

Reply via email to