Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt's QFontMetrics::horizontalAdvance() missing for Code Editor Implementation

I'm implementing a code editor for a simple scripting language and have been using the Qt Code Editor Example (http://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html) as a guide for implementing a QPlainTextEdit with line numbers. The example uses the base widget's fontMetrics() method 'horizontalAdvance' to query the width (or better the advance) of a single character.

The method QFontMetrics::horizontalAdvance(QChar) (see http://doc.qt.io/qt-5/qfontmetrics.html#horizontalAdvance-1) can be found in the Qt documentation but my implementation seems to be missing this method.

I am using Qt 5.10.1 'msvc2017_64' on Windows 10 with Visual Studio Enterprise 2017 Version 15.7.3. I have searched the headers for the missing method, but only found QTextLine::horizontalAdvance() which is not what I need.

I have replaced the QFontMetrics::horizontalAdvance(QChar) call with a call to QFontMetrics::width(QChar) which is working with my current font (Consolas) but I am not sure if its ok for every possible font.

Question 1) Has QFontMetrics::horizontalAdvance() been removed from current version of Qt, is my installation buggy or have I missed something?

Question 2) What is the correct way of getting the advance for a specific (non propotional) font?

Thank you for reading this and trying to help!

like image 416
Andreas H. Avatar asked Jun 14 '18 19:06

Andreas H.


2 Answers

As G.M. told me in a comment:

The documentation states that QFontMetrics::horizontalAdvance(QChar) wasn't available until Qt5.11.

So switching to Qt 5.11 fixes this problem.

like image 152
Andreas H. Avatar answered Nov 10 '22 23:11

Andreas H.


You might use width instead of horizontalAdvance.

The member function width is listed in Obsolete Members for QFontMetrics and it suggests to use horizontalAdvance instead of width. I guess these two member functions have a similar behaviour.

like image 34
kamae Avatar answered Nov 10 '22 21:11

kamae