当前位置:网站首页>qt颜色与字符串、uint相互转换
qt颜色与字符串、uint相互转换
2022-07-06 07:18:00 【码肥人壮】
软件开发中,很多设置需要使用json或者其他配置文件保存颜色值,把颜色拆成红、绿、蓝、透明度四个字段太麻烦了。
下面就是使用一个字段保存颜色的方法,就是将颜色保存为字符串或者uint类型,再将字符串解析成颜色。
首先分析一下,在很多json文件中颜色的使用如下字符表示:
#fffa3337
又或者,(上面的表示带透明度,下面不带)
#fa3337
读到程序中,他可以是字符串,也可以一段hex形式的uint值。
在qt中QRgb本质是unsigned int类型, qt源代码:
颜色转字符串
示范代码
//将颜色转为QRgb类型,color为QColor类型
QRgb mRgb = qRgba(color.red(), color.green(), color.blue(), color.alpha());
//将QRgb对象转为Hex字符串
obj["Color"] = QString::number(mRgb, 16);
输出样式如下:
"Color": "fffa3337",
字符串转颜色
示范代码
QString colorStr = obj["Color"].toString(); //获取颜色字符串
quint64 colorInt = colorStr.toUInt(NULL,16);
lineTemple.color = QColor(colorInt);
lineTemple.color.setAlpha(colorInt>>24);
边栏推荐
- Idea console color log
- leetcode1020. Number of enclaves (medium)
- TypeScript 函数定义
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- OpenJudge NOI 2.1 1749:数字方格
- The differences and advantages and disadvantages between cookies, seeion and token
- The psychological process from autojs to ice fox intelligent assistance
- Markdown 中设置图片图注
- Supervisor usage document
- Multi attribute object detection on rare aircraft data sets: experimental process using yolov5
猜你喜欢
随机推荐
word怎么只删除英语保留汉语或删除汉语保留英文
Idea console color log
The psychological process from autojs to ice fox intelligent assistance
Configure raspberry pie access network
杰理之BLE【篇】
Memory error during variable parameter overload
The differences and advantages and disadvantages between cookies, seeion and token
Lesson 12 study notes 2022.02.11
#systemverilog# 可綜合模型的結構總結
C - Inheritance - hidden method
TypeScript void 基础类型
#systemverilog# 可综合模型的结构总结
多线程和并发编程(二)
What does UDP attack mean? UDP attack prevention measures
Leetcode 78: subset
“无聊猿” BAYC 的内忧与外患
LeetCode Algorithm 2181. 合并零之间的节点
OpenJudge NOI 2.1 1661:Bomb Game
LeetCode 78:子集
leetcode59. 螺旋矩阵 II(中等)









