c++qtqt5

Qt 5.3 QPlainTextEdit Change the QTextCursor color


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


Solution

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