9

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!

Andreas H.
  • 1,757
  • 12
  • 24
  • 4
    The [documentation](http://doc.qt.io/qt-5/qfontmetrics.html#horizontalAdvance-1) states that `QFontMetrics::horizontalAdvance(QChar)` wasn't available until Qt5.11. – G.M. Jun 14 '18 at 19:54
  • Oh yes! I tought the code editor example has been around for a while and did not even check in this direction. Thanks a lot! I would accept your answer if you write one :) – Andreas H. Jun 14 '18 at 20:21

2 Answers2

12

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.

Melebius
  • 6,183
  • 4
  • 39
  • 52
Andreas H.
  • 1,757
  • 12
  • 24
5

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.

kamae
  • 1,825
  • 1
  • 16
  • 21