PDA

View Full Version : Hex Zero



johnmauer
17th June 2010, 13:45
Using QLineEdit::setHtml with font changes requires "#rrggbb" for arbitrary colors. Each QColor can be broken into the requisite subset easily, and then converted to a hex string. However, the string for zero in hex should be "00" yet the following code gives "0". The workaround is obvious but I am curious as to the reason if anyone knows.

QString hexZero = QString::number(0, 16);
Perhaps there is a better way to get the hex string for a given QColor, and I'm just too blind to see it.

Ginsengelf
17th June 2010, 15:01
Hi, you can use one of the QString::arg() methods. They allow to specify a minimum width of the result.

Ginsengelf

johnmauer
17th June 2010, 15:23
Works great. Thanks.


QString fontColorSet = "#%1%2%3";
fontColorSet = fontColorSet.arg(QString::number(fontColor.red(), 16), 2,'0')
.arg(QString::number(fontColor.green(), 16) ,2,'0')
.arg(QString::number(fontColor.blue(), 16),2,'0');