我正在使用QPlainTextEdit
和QSyntaxHighlighter
制作自定义代码编辑器,但我遇到了一个问题。我想要在选定文本中保留语法高亮。然而,选择的颜色(环境颜色)会覆盖由QSyntaxHighlighter
和html标签高亮的文本颜色。其他属性,如字体族,将被保留。
例子:
未选中: 选中:
(我希望Hello
变成绿色,World!
保持黑色)
我也尝试过将样式表设置为:
QPlainTextEdit {
selection-color: rgba(0, 0, 0, 0);
selection-background-color: lightblue;
}
结果:
背景色会遮盖文本和颜色,而且当 alpha = 0
时,文本颜色就不可见了。我之前这么做只是为了排除语法颜色在 selection-color
下仍然存在的想法。实际上,它被 selection-background-color
遮盖了。
编辑: 不,如果我同样将 selection-background-color
设置为 rgba(0, 0, 0, 0)
,就没有选择内容,也没有在选择内容中看到文本。我只能看到背景。
下面代码片段中使整个光标行高亮的方法似乎是可行的,但我基本上不得不重新实现所有的选择机制...
QList<QTextEdit::ExtraSelection> extraSelections;
QTextCursor cursor = textCursor();
QTextEdit::ExtraSelection selection;
selection.format.setBackground(lineHighlightColor_);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = cursor;
selection.cursor.clearSelection();
extraSelections.append(selection);
setExtraSelections(extraSelections);
有没有更简单的解决方案?
QTextEdit::ExtraSelection
是否可行),我会分享的。 - LogicStuff