python QLineEdit Text Color

user3279899 picture user3279899 · Dec 11, 2014 · Viewed 37.4k times · Source

I am trying to create a demonstration app to show how to change font colors.

I can do it in QLabel and QTextEdit

I have found no way to change the foreground text color for a QLineEdit.

The only thing I've tried that does not throw an error is:

color = QColorDialog.getColor(defaultHost.textColor(), pWidget, 'Get Text Color')
myPalette.setColor(myPalette.WindowText, QColor(color))

But, the text color remains black...

Is it or is it not possible to do this?

Answer

101 picture 101 · Dec 12, 2014

You can do it by setting the object's style sheet:

self.my_line_edit = QtGui.QLineEdit()

self.my_line_edit.setStyleSheet("color: red;")

# or

self.my_line_edit.setStyleSheet("color: rgb(255, 0, 0);")

# or

self.my_line_edit.setStyleSheet("""
    QLabel {
        color: red;
    }
    """)