Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multilingual QPlainTextEdit: Different font height
QtWS25 Last Chance

Multilingual QPlainTextEdit: Different font height

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    songziming
    wrote on last edited by
    #1

    I use QPlainTextEdit to implement a code editor, but english and CJK characters have different line height.

    I set the font as Ubuntu Mono which does not contain CJK characters, so Qt fallback to the WenQuanYi Zen Hei on my machine. I used the following code to inspect these two fonts.

    @QFont f1("Ubuntu Mono", 12);
    QFont f2("WenQuanYi Zen Hei", 12);
    fontInfo(f1);
    fontInfo(f2);@

    The function fontInfo is defined as

    @void fontInfo(QFont &f) {
    QFontMetrics m(f);
    cout << "Ascent: " << m.ascent() << ", Descent: " << m.descent() << ", Height: " << m.height() << ", Leading: " << m.leading() << ", LineSpacing: " << m.lineSpacing() << endl;
    cout << "Underline pos: " << m.underlinePos() << ", Overline pos: " << m.overlinePos() << ", Strikeline pos: " << m.strikeOutPos() << endl;
    }@

    The result is:

    Ubuntu Mono:
    Ascent: 14, Descent: 3, Height: 17, Leading: -1, LineSpacing: 16
    Underline pos: 2, Overline pos: 15, Strikeline pos: 4

    WenQuanYi Zen Hei
    Ascent: 16, Descent: 5, Height: 21, Leading: 1, LineSpacing: 22
    Underline pos: 4, Overline pos: 17, Strikeline pos: 5

    How can I set the line spacing to the same value so English and CJK characters would be same height.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Did you try with QTextFormat and setting the property QTextFormat::LineHeight ?

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        songziming
        wrote on last edited by
        #3

        I looked at the doc of QTextFormat and discovered that I have to implement a QSyntaxHighlighter to apply the format, so I came out with this:

        @class HighLighter : public QSyntaxHighlighter {
        Q_OBJECT
        public:
        explicit HighLighter(QObject *parent = 0) : QSyntaxHighlighter(parent) {}
        protected:
        void highlightBlock(const QString &text) {
        QTextCharFormat format;
        format.setProperty(QTextFormat::LineHeight, 50);
        this->setFormat(0, text.length(), format);
        this->setFormat(0, text.length(), Qt::blue);
        }
        };@

        The text successfully turned into blue, but the line height still not change.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I might be wrong but I don't think you can set a line height like that. Why don't you set that line height document wide ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved