18

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?

Andy
  • 49,085
  • 60
  • 166
  • 233
Mike Sr
  • 511
  • 1
  • 5
  • 15

5 Answers5

26

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;
    }
    """)
101
  • 8,514
  • 6
  • 43
  • 69
8

I solved for font text and background

 self.my_line_edit.setStyleSheet(
                """QLineEdit { background-color: green; color: white }""")
Hernan Ramirez
  • 625
  • 10
  • 11
3

Below is a code snippet that took me two days of trial and error to figure out. I hope it helps other newbies like myself. My comments in the code should help, too.

def set_palette(pWidget, pItem):
    # Get the pallet
    myPalette = pWidget.palette()
    defaultHost = led_dem.textEdit

    if isinstance(pWidget, QPushButton):
        # NOTE: Using stylesheets will temporarily change the color dialog popups push buttons
        print "Instance Is: %s " %(pWidget.objectName())
        # Existing colors.
        bgColor = pWidget.palette().color(QPalette.Background)
        fgColor = pWidget.palette().color(QPalette.Foreground)
        # Convert the QColors to a string hex for use in the Stylesheet.
        bg = bgColor.name()
        fg = fgColor.name()

        if pItem == 'Text':
            # Use the color dialog with a dummy widget to obtain a new QColor for the parameter we are changing.
            color = QColorDialog.getColor(defaultHost.textColor(), pWidget, 'Get Text Color')
            # Convert it to a string HEX
            fg = color.name()
            # Update all parameters of interest
            pWidget.setStyleSheet('background-color: ' + bg + ';color: ' + fg)

        if pItem == 'Background':
            color = QColorDialog.getColor(defaultHost.textColor(), pWidget, 'Get Background Color')
            myPalette.setColor(myPalette.Base, QColor(color))
            bg = color.name()
            pWidget.setStyleSheet('background-color: ' + bg + ';color: ' + fg)

This snippet shows:

  • how to find what type of widget you are dealing with;
  • how to covert a QColor from a QColorDialog into a string HEX format for use with a stylesheet; and
  • how to use the QColorDialog when the widget doesn't use a palette element of the type you need.

In my case I am using defaultHost = led_dem.textEdit where led_dem is my form and textEdit is a textEdit on the form.

Also, pWidget is the complete widget definition including form and instance.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Mike Sr
  • 511
  • 1
  • 5
  • 15
  • I'm curious to know why you spent all that time on this answer, when you'd already been given a one-line solution to your original question. But I suspect that your continuing difficulties on this subject stem from a failure to understand what the various [color roles](http://qt-project.org/doc/qt-4.8/qpalette.html#ColorRole-enum) do - in particular, `Background` and `Foreground` (which are both obsolete) aren't used in the way that you think they are (you should be using `Base` and `Text`, instead). – ekhumoro Dec 14 '14 at 19:36
  • 1
    "A Failure To Understand" You are right on target. I have found QT to be a somewhat confusing 'discipline'. And, there is no one where I am at that uses it at the level I do. I spent the time on the answer because by the time I got a response on here, I had (mostly) figured it out the hard way. – Mike Sr Dec 15 '14 at 11:30
  • Could not get 'Base' and 'Text' to work on a QPushbutton using Python 2.7.x and QT 4.8.5 – Mike Sr Dec 15 '14 at 12:55
  • Why would you think that `Base` and `Text` would work with a `QPushButton`? Your question is about changing the font colours in a `QLineEdit`. Please take the time to read the documentation for color roles that I linked to in my previous comment - the accompanying illustration could hardly make it clearer what roles you should be using for buttons. – ekhumoro Dec 15 '14 at 16:32
  • My apologies: I asked a question about a QLineEdit, and provided code for a QPushbutton. – Mike Sr Dec 16 '14 at 17:10
1

this is how I do it not using css

Palette= QtGui.QPalette()
Palette.setColor(QtGui.QPalette.Text, QtCore.Qt.red)
self.lineEdit.setPalette(Palette)

QLineEdit has a method initStyleOption and initStyleOption inherits QStyleOption, and then QStyleOption has a Method QPalette. Now you can take advatage of using QPalette methods.

you can visit this link for reference http://pyqt.sourceforge.net/Docs/PyQt4/qlineedit.html

fLY
  • 11
  • 3
0

For me this worked first try:
self.LBLDefteraStatusState.setStyleSheet('color: green;')