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. Change QLineEdit placeholder text color
Forum Updated to NodeBB v4.3 + New Features

Change QLineEdit placeholder text color

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 8 Posters 13.6k Views 3 Watching
  • 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.
  • M Offline
    M Offline
    Mr Gisa
    wrote on 25 Apr 2018, 20:03 last edited by
    #1

    Is it possible to change the placeholder of QLineEdit text color?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Apr 2018, 20:10 last edited by
      #2

      Hi,

      IIRC, change the QPalette of your QLineEdit and modify the QPalette::Text role with the color you want through setColor.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      5
      • M Offline
        M Offline
        Mr Gisa
        wrote on 25 Apr 2018, 20:13 last edited by
        #3

        Yeap, worked like a charm, thank you.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mr Gisa
          wrote on 25 Apr 2018, 20:30 last edited by Mr Gisa
          #4

          @SGaist I realized that if I change the color property in the stylesheet it will also change the placeholder text. I was wondering if it's possible to set the placeholder to a gray color and the text to another color.

          M D 2 Replies Last reply 25 Apr 2018, 20:34
          0
          • M Mr Gisa
            25 Apr 2018, 20:30

            @SGaist I realized that if I change the color property in the stylesheet it will also change the placeholder text. I was wondering if it's possible to set the placeholder to a gray color and the text to another color.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 25 Apr 2018, 20:34 last edited by mrjj
            #5

            @Mr-Gisa
            Well you would have to intercept QLineEdit getting focus and reset the role.
            either using a subclass + promotion ( to make it easy to use subclass)
            https://stackoverflow.com/questions/2804115/qlineedit-focus-event
            or you could use an event filter.

            Overengineered. sorry. @SGaist suggestion is much better.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 25 Apr 2018, 20:38 last edited by
              #6

              You can connect to the textChanged signal and change the palette based on the emptiness of the text.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M D 2 Replies Last reply 25 Apr 2018, 20:48
              2
              • S SGaist
                25 Apr 2018, 20:38

                You can connect to the textChanged signal and change the palette based on the emptiness of the text.

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 25 Apr 2018, 20:48 last edited by
                #7

                @SGaist
                Doh, yeah that would be far easier.

                1 Reply Last reply
                0
                • M Mr Gisa
                  25 Apr 2018, 20:30

                  @SGaist I realized that if I change the color property in the stylesheet it will also change the placeholder text. I was wondering if it's possible to set the placeholder to a gray color and the text to another color.

                  D Offline
                  D Offline
                  Devopia53
                  wrote on 26 Apr 2018, 02:05 last edited by Devopia53
                  #8

                  @Mr-Gisa

                  If you want to use QSS instead of QPalette, try the following:

                  setStyleSheet("QLineEdit{ color:red; }\nQLineEdit[text=\"\"]{ color:gray; }");
                  connect(lineEdit, &QLineEdit::textChanged, [=]{ style()->polish(lineEdit); });
                  
                  
                  M 1 Reply Last reply 26 Apr 2018, 04:41
                  2
                  • D Devopia53
                    26 Apr 2018, 02:05

                    @Mr-Gisa

                    If you want to use QSS instead of QPalette, try the following:

                    setStyleSheet("QLineEdit{ color:red; }\nQLineEdit[text=\"\"]{ color:gray; }");
                    connect(lineEdit, &QLineEdit::textChanged, [=]{ style()->polish(lineEdit); });
                    
                    
                    M Offline
                    M Offline
                    Mr Gisa
                    wrote on 26 Apr 2018, 04:41 last edited by
                    #9

                    @Devopia53 That is really good, thank you very much.

                    1 Reply Last reply
                    1
                    • S SGaist
                      25 Apr 2018, 20:38

                      You can connect to the textChanged signal and change the palette based on the emptiness of the text.

                      D Offline
                      D Offline
                      davidmorales0722
                      wrote on 17 Jan 2021, 23:52 last edited by
                      #10

                      @SGaist said in Change QLineEdit placeholder text color:

                      You can connect to the textChanged signal and change the palette based on the emptiness of the text.

                      But what can I do if I have several QLineEdit and I want to change the palette on each one of them... I know there's a better way to do this other than create slots for every LineEdit

                      J 1 Reply Last reply 18 Jan 2021, 07:08
                      0
                      • D davidmorales0722
                        17 Jan 2021, 23:52

                        @SGaist said in Change QLineEdit placeholder text color:

                        You can connect to the textChanged signal and change the palette based on the emptiness of the text.

                        But what can I do if I have several QLineEdit and I want to change the palette on each one of them... I know there's a better way to do this other than create slots for every LineEdit

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 18 Jan 2021, 07:08 last edited by
                        #11

                        @davidmorales0722 Simply connect a lambda to the signal and pass the line edit as parameter:

                        connect(lineEdit, &QLineEdit::textChanged1, [lineEdit, this]{ style()->polish(lineEdit1); });
                        connect(lineEdit, &QLineEdit::textChanged2, [lineEdit, this]{ style()->polish(lineEdit2); });
                        ...
                        

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • X Offline
                          X Offline
                          xmichelo 0
                          wrote on 23 Mar 2021, 13:44 last edited by xmichelo 0
                          #12

                          No need to connect a slot to a signal, you can modify the palette's QPalette::PlaceholderText color in Qt Designer, or programmatically with something like:

                          QPalette palette = edit.palette();
                          palette.setColor(QPalette::PlaceholderText, QColor(0, 0, 0, 50));
                          edit.setPalette(palette);
                          
                          H 1 Reply Last reply 4 Jan 2022, 06:57
                          2
                          • X xmichelo 0
                            23 Mar 2021, 13:44

                            No need to connect a slot to a signal, you can modify the palette's QPalette::PlaceholderText color in Qt Designer, or programmatically with something like:

                            QPalette palette = edit.palette();
                            palette.setColor(QPalette::PlaceholderText, QColor(0, 0, 0, 50));
                            edit.setPalette(palette);
                            
                            H Offline
                            H Offline
                            Hector_J
                            wrote on 4 Jan 2022, 06:57 last edited by Hector_J 1 Apr 2022, 06:57
                            #13

                            @xmichelo-0 this placeholder text color method did not have effect at UI first open, only puts some words on the lineEdit field and then delete it, it just work.
                            test on QT 6.2 with MacOS.

                            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