小编mvi*_*auz的帖子

QSyntaxHighlighter - 文本选择覆盖样式

我正在使用QPlainTextEdit和编写自定义代码编辑器,QSyntaxHighlighter我遇到了一个小故障.我想在选择中保留语法突出显示.但是,选择的颜色(环境颜色)会覆盖由QSyntaxHighlighterhtml标记突出显示的文本的颜色.保留字体系列等其他属性.


例:

没有选择:选择:
未选择      选
                                   (我想Hello变成绿色,World!变成黑色)


我也尝试将样式表设置为:

QPlainTextEdit {
    selection-color: rgba(0, 0, 0, 0);
    selection-background-color: lightblue;
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述
背景颜色覆盖文本,并且文本颜色alpha = 0不可见.我这样做只是为了排除语法颜色持续存在的想法selection-color.事实上它被覆盖了selection-background-color.
编辑:不,如果我也设置selection-background-colorrgba(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);
Run Code Online (Sandbox Code Playgroud)

有没有更简单的解决方案?

qt qplaintextedit qt5 qtstylesheets

21
推荐指数
1
解决办法
887
查看次数

标签 统计

qplaintextedit ×1

qt ×1

qt5 ×1

qtstylesheets ×1