Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QLineEdit change PlaceholderText color

I have a LineEdit widget in an app and its PlaceholderText changes based on the user's input. However, I'd like for the PlaceholderText to look like normal text, i.e. to be black instead of grey.

I've looked online but most of the results were either not precise enough for me to understand them or using different languages than Python, which makes it hard for me to implement the solution in my script.

like image 581
Thibaut B. Avatar asked Sep 20 '25 06:09

Thibaut B.


1 Answers

To change the color of the placeholderText then you have to use the QPalette:

import sys

from PyQt5 import QtGui, QtWidgets


def main():

    app = QtWidgets.QApplication(sys.argv)

    w = QtWidgets.QLineEdit(placeholderText="Stack Overflow")

    pal = w.palette()
    text_color = pal.color(QtGui.QPalette.Text)
    # or
    # text_color = QtGui.QColor("black")
    pal.setColor(QtGui.QPalette.PlaceholderText, text_color)
    w.setPalette(pal)

    w.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
like image 180
eyllanesc Avatar answered Sep 22 '25 19:09

eyllanesc