Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python QLineEdit Text Color

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?

like image 905
Mike Sr Avatar asked Dec 11 '14 21:12

Mike Sr


People also ask

How do I change the text color in QLineEdit?

In QC Designer, click on the QLineEdit box and in the properties, enable "autoFillBackground". Next, select the Palette property and in the pop up window, change the Text color to white.


2 Answers

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;
    }
    """)
like image 112
101 Avatar answered Sep 20 '22 01:09

101


I solved for font text and background

 self.my_line_edit.setStyleSheet(
                """QLineEdit { background-color: green; color: white }""")
like image 21
Hernan Ramirez Avatar answered Sep 19 '22 01:09

Hernan Ramirez