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 14.0k 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 last edited by
    #3

    Yeap, worked like a charm, thank you.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mr Gisa
      wrote on 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.

      mrjjM D 2 Replies Last reply
      0
      • M Mr Gisa

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

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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

          mrjjM D 2 Replies Last reply
          2
          • SGaistS SGaist

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #7

            @SGaist
            Doh, yeah that would be far easier.

            1 Reply Last reply
            0
            • M Mr Gisa

              @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 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
              2
              • D Devopia53

                @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 last edited by
                #9

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

                1 Reply Last reply
                1
                • SGaistS SGaist

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

                  jsulmJ 1 Reply Last reply
                  0
                  • D davidmorales0722

                    @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

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 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 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
                      2
                      • X xmichelo 0

                        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 last edited by Hector_J
                        #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