当前位置:网站首页>QT color is converted to string and uint

QT color is converted to string and uint

2022-07-06 07:22:00 Big and strong

Software development , Many settings need to be used json Or other configuration files save color values , Split the color into red 、 green 、 blue 、 Four fields of transparency are too troublesome .

Here's how to save colors using a field , Is to save the color as a string or uint type , Then parse the string into color .
So let's analyze this , In many json The colors in the file are represented by the following characters :

#fffa3337
 Or, ,( The above shows transparency , There is no )
#fa3337

Read into program , It can be a string , It can also be a period hex Formal uint value .

stay qt in QRgb The essence is unsigned int type , qt Source code :
 Insert picture description here

Color to string

Demonstration code

	// Turn the color to QRgb type ,color by QColor type 
    QRgb mRgb = qRgba(color.red(), color.green(), color.blue(), color.alpha());
    // take QRgb Object to Hex character string 
    obj["Color"] = QString::number(mRgb, 16);

The output style is as follows :

"Color": "fffa3337",

String to color

Demonstration code

   QString colorStr = obj["Color"].toString();	// Get color string 
   quint64 colorInt = colorStr.toUInt(NULL,16);
   lineTemple.color = QColor(colorInt);
   lineTemple.color.setAlpha(colorInt>>24);
原网站

版权声明
本文为[Big and strong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060718139468.html