当前位置:网站首页>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);
边栏推荐
- 开源的网易云音乐API项目都是怎么实现的?
- Zhongqing reading news
- Path analysis model
- Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
- 变量的命名规则十二条
- Twelve rules for naming variables
- Uni app third party package configuration network request
- Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
- #systemverilog# 可综合模型的结构总结
- Hydra common commands
猜你喜欢
随机推荐
树莓派3B更新vim
The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
NiO programming introduction
word中把带有某个符号的行全部选中,更改为标题
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
OpenGL ES 学习初识(1)
1091: two or three things in childhood (multi instance test)
How to configure GUI guide development environment
C - Inheritance - polymorphism - virtual function member (lower)
Supervisor usage document
Bloom taxonomy
Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
Multi attribute object detection on rare aircraft data sets: experimental process using yolov5
升级版手机检测微信工具小程序源码-支持多种流量主模式
leetcode704. 二分查找(查找某个元素,简单,不同写法)
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
How are the open source Netease cloud music API projects implemented?
Go learning --- use reflection to judge whether the value is valid
Raspberry pie serial port login and SSH login methods
Crawling exercise: Notice of crawling Henan Agricultural University









