我想增加QPlainTextEdit中的段落(文本块)之间的间距,但无济于事。经过实验后,我发现尽管某些格式属性(例如背景色)生效,但其他属性(例如页边距)却被忽略了。
我找到了this bug report,但其中只提到了datalist.forEach(function(name) {
console.log(name.onSelection);
var keytoFind = name.onSelection;
var index = Object.keys(datalist).indexOf(keytoFind);
alert(index);
});
。就我而言,QTextBlockFormat::lineHeight()
的几乎所有方法都将被忽略。一个最小的例子:
QTextBlockFormat::*
除#include <QtWidgets>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QPlainTextEdit te;
te.setPlainText("block1\nblock2\nblock3");
QTextCursor cursor = te.textCursor();
cursor.select(QTextCursor::Document);
QTextBlockFormat fmt;
fmt.setBackground(QColor(Qt::yellow));
fmt.setBottomMargin(600);
fmt.setIndent(20);
fmt.setTopMargin(600);
fmt.setLeftMargin(40);
cursor.setBlockFormat(fmt);
te.show();
return a.exec();
}
外,其他所有字符均被忽略。使用Qt 5.10。
答案 0 :(得分:0)
请勿使用QPlainTextEdit
对文本进行密集格式化。请使用其表亲 QTextEdit
。
使用您的代码并将其从QPlainTextEdit
更改为 QTextEdit
,即可解决此问题。
仅供参考,您链接的Bug Report中也提到了这一点。也许您错过了它,或者忘记了在问题中提及它?
用QTextEdit替换QPlainTextEdit将显示预期的结果。
QTextEdit te; // note the change in type
te.setPlainText("block1\nblock2\nblock3");
QTextCursor cursor = te.textCursor();
cursor.select(QTextCursor::Document);
QTextBlockFormat fmt;
fmt.setBackground(QColor(Qt::yellow));
fmt.setBottomMargin(6); // let's not overexaggerate anything
fmt.setIndent(1);
fmt.setTopMargin(12);
fmt.setLeftMargin(1);
cursor.setBlockFormat(fmt);
这是在MacOS上使用QPlainTextEdit
的结果。
这是在MacOS上使用QTextEdit
的结果。
请注意,不需要修改任何成员函数,所以这是一个加号。从QPlainTextEdit
到QTextEdit
的过渡并不难。
进一步阅读:This other SO post有点帮助。如果您打算使用更多的Qt文本类,那么也很容易阅读。