Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Change text cursor color in QTextEdit
Forum Update on Monday, May 27th 2025

[Solved]Change text cursor color in QTextEdit

Scheduled Pinned Locked Moved General and Desktop
14 Posts 3 Posters 16.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Acce0ss
    wrote on last edited by
    #3

    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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Macro
      wrote on last edited by
      #4

      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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Macro
        wrote on last edited by
        #5

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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Macro
          wrote on last edited by
          #6

          !http://img546.imageshack.us/img546/296/mainwindowr.png(Text Edit with White Color and Text Cursor with White Color )!

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Acce0ss
            wrote on last edited by
            #7

            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.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              AlekseyOk
              wrote on last edited by
              #8

              use style sheet as Riz proposed

              @
              QTextEdit
              {
              color: white;
              background-color: black;
              }
              @

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Acce0ss
                wrote on last edited by
                #9

                Here is the code I used to change the colors:

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

                textEdit->setFrameShape(QFrame::NoFrame);
                textEdit->setTextColor(QColor(255,255,255));
                @
                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.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  AlekseyOk
                  wrote on last edited by
                  #10

                  Yes, you can change style sheet at runtime:

                  @
                  QFile file("style.css");
                  if(file.open(QFile::ReadOnly))
                  {
                  QString styleSheet(file.readAll());
                  widget->setStyleSheet(styleSheet);
                  file.close();
                  }
                  @

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Acce0ss
                    wrote on last edited by
                    #11

                    Thanks Riz and Aleksey, it works now, using this
                    @
                    textEdit->setStyleSheet(
                    "QTextEdit"
                    "{"
                    "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.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Macro
                      wrote on last edited by
                      #12

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

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Macro
                        wrote on last edited by
                        #13

                        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..

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          Macro
                          wrote on last edited by
                          #14

                          If you are using setTextColor(),

                          @TextEdit->setTextColor(Qt::white);@

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

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved