Qt 5.3 QPlainTextEdit Change QTextCursor color

I would like to change the color of the cursor below the widget QPlainTextEdit

. I was able to set the width to 6

, but I want to change the color or it. Is it possible?

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

      

Thank.

Edit:

Including images for illustration ...

From this:

Initial Cursor Color

For this:

enter image description here

Thank.

Edit2: Final Look

enter image description here

+3


source to share


2 answers


You can use QTextCharFormat

to set the text color to QPlainTextEdit

. Use QTextCharFormat::setForeground

to set color. Then use a stylesheet to change the cursor color using the property color

.



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);

      

+3


source


Edit

You can change the caret color with the following style sheet:

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

      

But there is another problem: all text also becomes with this color. How do I set a new text color but keep the old caret color? I found this solution, probably not the best one: use html code:

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

      

Result (since you want to have the original color for the caret and for the text):

enter image description here



The text should now have a color, but the caret has a special color. This is a solution, but it is a little messy for me, if anyone finds a better way to change the text color without changing the caret color, please say so.

You can only change the main pointer under the widgets:

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

      

Qt has the following built-in cursors: http://qt-project.org/doc/qt-5/qt.html#CursorShape-enum

Qt doesn't have any specific color cursor, so you must use your own pixmap. You can use an image with a simple arrow, but with a different color (if it has an alpha channel, then this is better)

There are many different cursors on the Internet.

+3


source







All Articles