我正在使用QPlainTextEdit
and制作自定义代码编辑器,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);
有没有更简单的解决方案?