Thibaut B.
Thibaut B.

Reputation: 321

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.

Upvotes: 4

Views: 1956

Answers (1)

eyllanesc
eyllanesc

Reputation: 244301

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

Upvotes: 3

Related Questions