Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt 5.3 QPlainTextEdit Change the QTextCursor color

Tags:

c++

qt

qt5

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 ?

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

Thanks.

Edit:

Including the images to exemplify...

From this:

Initial Cursor Color

To this:

enter image description here

Thanks.

Edit2: Final Look

enter image description here

like image 300
Yore Avatar asked Oct 28 '14 18:10

Yore


1 Answers

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.

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);
like image 95
thuga Avatar answered Sep 22 '22 16:09

thuga