开发者

How to Convert Qcolor value to hex value?

开发者 https://www.devze.com 2023-02-03 02:24 出处:网络
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 QColo

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 = 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消