当前位置:网站首页>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);
边栏推荐
- leetcode841. Keys and rooms (medium)
- 网络安全基础介绍
- Zhongqing reading news
- Leetcode 78: subset
- Memory error during variable parameter overload
- JDBC学习笔记
- supervisor 使用文档
- [online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code
- 作者已死?AI正用艺术征服人类
- Simple use of JWT
猜你喜欢
What is the biggest problem that fresh e-commerce is difficult to do now
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
配置树莓派接入网络
树莓派串口登录与SSH登录方法
SSM学习
The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
leetcode841. Keys and rooms (medium)
杰理之如若需要大包发送,需要手机端修改 MTU【篇】
Thought map of data warehouse construction
Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
随机推荐
呆错图床系统源码图片CDN加速与破解防盗链功能
TS基础篇
MVVM of WPF
LeetCode Algorithm 2181. Merge nodes between zero
Uni app practical project
leetcode59. 螺旋矩阵 II(中等)
【mysql学习笔记30】锁(非教程)
作者已死?AI正用藝術征服人類
Structure summary of SystemVerilog integrable model
Top test sharing: if you want to change careers, you must consider these issues clearly!
mysql如何合并数据
Missing monitoring: ZABBIX monitors the status of Eureka instance
leetcode704. 二分查找(查找某个元素,简单,不同写法)
数字IC设计笔试题汇总(一)
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
word设置目录
supervisor 使用文档
How MySQL merges data
Raspberry pie serial port login and SSH login methods
杰理之蓝牙设备想要发送数据给手机,需要手机先打开 notify 通道【篇】