Important: Please read the Qt Code of Conduct -

[Solved]Change text cursor color in QTextEdit

  • Hello,

    I need to have black background color and white text in a text editor. I have achieved this, but I cannot find a way to change the text cursor color to white and therefore the black cursor is invisible.

    Is there a way to do this?.

  • If the Text Color is White in color then obviously the Text Cursor Color will also be White in color. You need to change the Text Cursor Color alone to Black, keeping the Text Color in White itself ? Please be clear with that.

  • yes, i think that if change the text color, then the text cursor obviously should change color too. But that doesn't happen.

    so I have black background, and I have changed the text color to white, BUT the cursor color doesn't change to the same color as the text and is therefore invisible. Which is why asked if it is explictly possible to change the cursor color.

    to clarify, here is my desired setup:
    black background, white text, white cursor.

    and what actually happens after changing the colors:
    black background, white text, black cursor.

  • Did u tried to change the Stylesheet by using Ui Designer or by Coding ? I Tried it in Designer and it Works Fine for me...

    Thanks & Regards

  • I will update the screen shot of my output for your reference..

  • ! Edit with White Color and Text Cursor with White Color )!

  • I used coding, modified the demo called textedit.

    I will update the code I used for color change and so on when I get back home.

  • use style sheet as Riz proposed

    color: white;
    background-color: black;

  • Here is the code I used to change the colors:

    @QPalette p = textEdit->palette();
    p.setColor(QPalette::Base, QColor(0, 0, 0));

    If I use a style sheet, is it possible to change the colors on the run too? The background color won't change but text color might.

  • Yes, you can change style sheet at runtime:

    QFile file("style.css");
    QString styleSheet(file.readAll());

  • Thanks Riz and Aleksey, it works now, using this
    "color: white;"
    "background-color: black;"

    So the cursor color can be set by setting color: whatever in a stylesheet.. But apparently not with setTextColor() or any other function of textEdit.. weird, but I'm glad it works now.

  • Yeah Aleksey0k is Right...!!! We can change the StyleSheet at Runtime too....

  • Your Welcome.. Happy Coding... :) Simple way of coding is..

    @TextEdit->setStyleSheet("background-color: black; color : white");@

    Thats all.... No Need for going for a QPalette and all..

  • If you are using setTextColor(),


    will change the Text Color Alone. But the Text Cursor will remain the default Black in color...

Log in to reply