Change QLineEdit placeholder text color
-
wrote on 25 Apr 2018, 20:03 last edited by
Is it possible to change the placeholder of
QLineEdit
text color? -
-
wrote on 25 Apr 2018, 20:13 last edited by
Yeap, worked like a charm, thank you.
-
@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.@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. -
You can connect to the textChanged signal and change the palette based on the emptiness of the text.
-
You can connect to the textChanged signal and change the palette based on the emptiness of the text.
@SGaist
Doh, yeah that would be far easier. -
@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.wrote on 26 Apr 2018, 02:05 last edited by Devopia53If 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); });
-
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); });
wrote on 26 Apr 2018, 04:41 last edited by@Devopia53 That is really good, thank you very much.
-
You can connect to the textChanged signal and change the palette based on the emptiness of the text.
wrote on 17 Jan 2021, 23:52 last edited by@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
-
@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
@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); }); ...
-
wrote on 23 Mar 2021, 13:44 last edited by 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);
-
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);
wrote on 4 Jan 2022, 06:57 last edited by Hector_J 1 Apr 2022, 06:57@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.