Multilingual QPlainTextEdit: Different font height
-
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: 4WenQuanYi Zen Hei
Ascent: 16, Descent: 5, Height: 21, Leading: 1, LineSpacing: 22
Underline pos: 4, Overline pos: 17, Strikeline pos: 5How can I set the line spacing to the same value so English and CJK characters would be same height.
-
Hi and welcome to devnet,
Did you try with QTextFormat and setting the property QTextFormat::LineHeight ?
Hope it helps
-
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.
-
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 ?