【发布时间】:2015-03-31 04:13:40
【问题描述】:
我正在使用 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),则没有选择,并且该选择中没有文本。我看到的只是背景。
以下 sn-p 的方法使整个光标的行突出显示似乎是可行的方法,但我基本上最终会重新实现所有选择机制...
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);
有没有更简单的解决方案?
【问题讨论】:
-
你有没有找到更简单的解决方案?
-
@NicolasHolthaus 我已经辞职了。如果我找到了一种更简单的方法(我不知道
QTextEdit::ExtraSelection是否是一个可行的解决方案),我会分享它。 -
这是语法高亮的一个非常烦人的限制。
标签: qt qt5 qtstylesheets qplaintextedit