How to Convert Qcolor value to hex value?

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.


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

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 =

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.

Need Your Help

How do I remove code duplication between similar const and non-const member functions?

c++ class const code-duplication c++-faq

Let's say I have the following class X where I want to return access to an internal member: