Change QLineEdit placeholder text color
-
@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 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. -
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); });
@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.
@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); }); ...
-
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);
@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.