32

I have implemented a QColor dialog box which opens on certain condition. To get the selected color after final selection I use the method selectedColor() which returns the value in QColor. When I print that value, it's like this:

<PyQt4.QtGui.QColor object at 0x01DD7880>

I want color value in hex value like this: #DFDFDF (for grey). If it's not hex, correct me.

Is there any function to convert that?

Any suggestions welcome.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
vettipayyan
  • 3,150
  • 3
  • 25
  • 34

2 Answers2

48

You need to print selectedColor().name() to print the actual color value in hex. See the QColor Documentation

Jason B
  • 12,835
  • 2
  • 41
  • 43
3

To amplify a bit, maybe confuse, maybe clarify... (For Python newbies)

color = QColorDialog.getColor(pWidget.textBackgroundColor(), pWidget, 'Get Text Highlighting Color')

The above will return a QColor using the QColorDialog, for those of us who don't want to be stuck with named colors like 'Blue', 'red', green etc.

fg = color.name()

In this case I am converting the QColor to a string HEX for use in a style sheet.

Widget.setStyleSheet('background-color: ' + bg + ';color: ' + fg)

This is how such a converted value can be used in a style sheet.

Note how to concatenate more than one stylesheet attribute. Also, side note, sometimes changing one attribute cancels previous changes to others.

IndustProg
  • 627
  • 1
  • 13
  • 33
Mike Sr
  • 511
  • 1
  • 5
  • 15
  • Note: QColorDialog.getColor will fail if the attribute, in this case pWidget.testBackGroundColor, doesn't exist in the widget. You can use any other exisiting widget's color attribute. – Mike Sr Dec 14 '14 at 09:49