Search code examples
c++qtvisual-studio-2017qt5.10

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!


Solution

  • 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.