当前位置:网站首页>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);
边栏推荐
- [JDBC] quick start tutorial
- SEO学习的最好方式:搜索引擎
- Configure raspberry pie access network
- Go learning -- implementing generics based on reflection and empty interfaces
- [some special grammars about C]
- Multithreading and concurrent programming (2)
- 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
- Bloom taxonomy
- Memory error during variable parameter overload
- C - Inheritance - hidden method
猜你喜欢
随机推荐
How are the open source Netease cloud music API projects implemented?
多线程和并发编程(二)
Go learning --- use reflection to judge whether the value is valid
位运算异或
navicat如何导入MySQL脚本
MVVM of WPF
TS基础篇
Leetcode35. search the insertion position (simple, find the insertion position, different writing methods)
word设置目录
LeetCode Algorithm 2181. Merge nodes between zero
Simple and understandable high-precision addition in C language
Oracle database 11gr2 uses TDE transparent data encryption to report an error ora28353. If you run to close the wallet, you will report an error ora28365. If you run to open the wallet, you will repor
杰理之蓝牙设备想要发送数据给手机,需要手机先打开 notify 通道【篇】
巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
Chrome view page FPS
Openjudge noi 2.1 1749: Digital Square
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
数据仓库建设思维导图
数字IC设计笔试题汇总(一)
LeetCode 78:子集