改变QTextCursor的颜色

时间:2020-12-20 23:11:19

I would like to change the cursor color under the QPlainTextEdit widget. I was able to set it's width to 6, but I want to change the color or it. Is it possible ?

我想在QPlainTextEdit小部件下更改光标颜色。我可以把宽度设为6,但我想改变颜色。是可能的吗?

QFontMetrics fm(font());
setCursorWidth( fm.averageCharWidth() );
//setCursorColor is what I need.

Thanks.

谢谢。

Edit:

编辑:

Including the images to exemplify...

包括图片来举例说明……

From this:

从这个:

改变QTextCursor的颜色

To this:

:

改变QTextCursor的颜色

Thanks.

谢谢。

Edit2: Final Look

Edit2:最后看

改变QTextCursor的颜色

2 个解决方案

#1


3  

You can use QTextCharFormat to set the color of the text in your QPlainTextEdit. Use the QTextCharFormat::setForeground to set the color. Then use a stylesheet to change the color of the cursor by using the color property.

您可以使用QTextCharFormat设置QPlainTextEdit中的文本的颜色。使用QTextCharFormat: set前台设置颜色。然后使用样式表通过颜色属性改变光标的颜色。

QPlainTextEdit *p_textEdit = new QPlainTextEdit;
p_textEdit->setStyleSheet("QPlainTextEdit{color: #ffff00; background-color: #303030;"
                          " selection-background-color: #606060; selection-color: #ffffff;}");
QTextCharFormat fmt;
fmt.setForeground(QBrush(QColor(255,255,255)));
p_textEdit->mergeCurrentCharFormat(fmt);

#2


3  

Edit

编辑

You can change caret color with next stylesheet:

您可以使用下一个样式表更改插入符号的颜色:

ui->plainTextEdit->setStyleSheet(
            "QPlainTextEdit"
            "{"
            "color: yellow;"
            "}"
            );

But there is another problem, all text becomes with this color too. How to set new color to text but left old color for caret? I found this solution, maybe not the best: use html code:

但是还有另一个问题,所有的文字都变成了这个颜色。如何为文本设置新颜色,而为插入符号保留旧颜色?我发现了这个解决方案,也许不是最好的:使用html代码:

ui->plainTextEdit->appendHtml("<font color = \"red\"> Sample Text</font>");

Result (as you want original color for caret and for text):

结果(你想要插入符号和文本的原色):

改变QTextCursor的颜色

Now text has needed color but caret has special color. It is solution, but it is a little dirty for me, if someone will find better way to change color of text without changing caret color, please tell this.

现在文本需要颜色,但是caret有特殊的颜色。这是解决方案,但是对我来说有点脏,如果有人能找到更好的方法在不改变插入符号颜色的情况下改变文本的颜色,请告诉我。

You can change only main cursor under widget:

您只能在小部件下更改主光标:

QPixmap pix("pathToPixmap");
QCursor cur(pix);
ui->plainTextEdit->viewport()->setCursor(cur);

Qt has next embedded cursors: http://qt-project.org/doc/qt-5/qt.html#CursorShape-enum

Qt有下一个嵌入式游标:http://qt-project.org/doc/qt-5/qt.html#CursorShape-enum

Qt has not any cursor with specific color, so you should use you own pixmap. You can use image with simple arrow but with another color (if it has alpha-channel then it is better)

Qt没有任何特定颜色的游标,所以您应该使用自己的pixmap。你可以使用简单的箭头,但是用另一种颜色(如果它有alpha通道,那就更好了)

There are many different cursors in the web.

web中有许多不同的游标。

#1


3  

You can use QTextCharFormat to set the color of the text in your QPlainTextEdit. Use the QTextCharFormat::setForeground to set the color. Then use a stylesheet to change the color of the cursor by using the color property.

您可以使用QTextCharFormat设置QPlainTextEdit中的文本的颜色。使用QTextCharFormat: set前台设置颜色。然后使用样式表通过颜色属性改变光标的颜色。

QPlainTextEdit *p_textEdit = new QPlainTextEdit;
p_textEdit->setStyleSheet("QPlainTextEdit{color: #ffff00; background-color: #303030;"
                          " selection-background-color: #606060; selection-color: #ffffff;}");
QTextCharFormat fmt;
fmt.setForeground(QBrush(QColor(255,255,255)));
p_textEdit->mergeCurrentCharFormat(fmt);

#2


3  

Edit

编辑

You can change caret color with next stylesheet:

您可以使用下一个样式表更改插入符号的颜色:

ui->plainTextEdit->setStyleSheet(
            "QPlainTextEdit"
            "{"
            "color: yellow;"
            "}"
            );

But there is another problem, all text becomes with this color too. How to set new color to text but left old color for caret? I found this solution, maybe not the best: use html code:

但是还有另一个问题,所有的文字都变成了这个颜色。如何为文本设置新颜色,而为插入符号保留旧颜色?我发现了这个解决方案,也许不是最好的:使用html代码:

ui->plainTextEdit->appendHtml("<font color = \"red\"> Sample Text</font>");

Result (as you want original color for caret and for text):

结果(你想要插入符号和文本的原色):

改变QTextCursor的颜色

Now text has needed color but caret has special color. It is solution, but it is a little dirty for me, if someone will find better way to change color of text without changing caret color, please tell this.

现在文本需要颜色,但是caret有特殊的颜色。这是解决方案,但是对我来说有点脏,如果有人能找到更好的方法在不改变插入符号颜色的情况下改变文本的颜色,请告诉我。

You can change only main cursor under widget:

您只能在小部件下更改主光标:

QPixmap pix("pathToPixmap");
QCursor cur(pix);
ui->plainTextEdit->viewport()->setCursor(cur);

Qt has next embedded cursors: http://qt-project.org/doc/qt-5/qt.html#CursorShape-enum

Qt有下一个嵌入式游标:http://qt-project.org/doc/qt-5/qt.html#CursorShape-enum

Qt has not any cursor with specific color, so you should use you own pixmap. You can use image with simple arrow but with another color (if it has alpha-channel then it is better)

Qt没有任何特定颜色的游标,所以您应该使用自己的pixmap。你可以使用简单的箭头,但是用另一种颜色(如果它有alpha通道,那就更好了)

There are many different cursors in the web.

web中有许多不同的游标。