简体   繁体   English

代码编辑器实现缺少 Qt 的 QFontMetrics::horizo​​ntalAdvance()

[英]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.我正在为一种简单的脚本语言实现一个代码编辑器,并且一直在使用 Qt 代码编辑器示例(http://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html )作为指导使用行号实现 QPlainTextEdit。 The example uses the base widget's fontMetrics() method 'horizontalAdvance' to query the width (or better the advance) of a single character.该示例使用基本小部件的 fontMetrics() 方法 'horizo​​ntalAdvance' 来查询单个字符的宽度(或更好的前进)。

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 .方法QFontMetrics::horizontalAdvance(QChar) (参见http://doc.qt.io/qt-5/qfontmetrics.html#horizo​​ntalAdvance-1 )可以在 Qt 文档中找到,但我的实现似乎缺少这种方法

I am using Qt 5.10.1 'msvc2017_64' on Windows 10 with Visual Studio Enterprise 2017 Version 15.7.3.我在 Windows 10 上使用 Qt 5.10.1 'msvc2017_64' 和 Visual Studio Enterprise 2017 版本 15.7.3。 I have searched the headers for the missing method, but only found QTextLine::horizontalAdvance() which is not what I need.我已经搜索了缺少方法的标题,但只找到了QTextLine::horizontalAdvance()这不是我需要的。

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.我已将QFontMetrics::horizontalAdvance(QChar)调用替换为对QFontMetrics::width(QChar) QFontMetrics::horizontalAdvance(QChar)调用,该调用与我当前的字体 (Consolas) 一起使用,但我不确定它是否适用于每种可能的字体。

Question 1) Has QFontMetrics::horizontalAdvance() been removed from current version of Qt, is my installation buggy or have I missed something?问题 1) QFontMetrics::horizo​​ntalAdvance() 是否已从 Qt 的当前版本中删除,是我的安装有问题还是遗漏了什么?

Question 2) What is the correct way of getting the advance for a specific (non propotional) font?问题 2) 获得特定(非比例)字体预付款的正确方法是什么?

Thank you for reading this and trying to help!感谢您阅读本文并尝试提供帮助!

As GM told me in a comment:正如GM在评论中告诉我的那样:

The documentation states that QFontMetrics::horizontalAdvance(QChar) wasn't available until Qt5.11 . 文档指出 QFontMetrics::horizo​​ntalAdvance(QChar) 直到Qt5.11 才可用

So switching to Qt 5.11 fixes this problem.所以切换到 Qt 5.11 解决了这个问题。

You might use width instead of horizontalAdvance .您可以使用width而不是horizontalAdvance

The member function width is listed in Obsolete Members for QFontMetrics and it suggests to use horizontalAdvance instead of width .成员函数widthOsolete Members for QFontMetrics 中列出,它建议使用horizontalAdvance而不是width I guess these two member functions have a similar behaviour.我猜这两个成员函数有类似的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM