当前位置:网站首页>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);
边栏推荐
- C - Inheritance - hidden method
- [some special grammars about C]
- navicat如何导入MySQL脚本
- 网络安全基础介绍
- Bloom taxonomy
- 杰理之需要修改 gatt 的 profile 定义【篇】
- UWA pipeline version 2.2.1 update instructions
- Simple and understandable high-precision addition in C language
- First knowledge of OpenGL es learning (1)
- word中把带有某个符号的行全部选中,更改为标题
猜你喜欢
Go learning -- implementing generics based on reflection and empty interfaces
Hydra common commands
Top test sharing: if you want to change careers, you must consider these issues clearly!
leetcode704. 二分查找(查找某个元素,简单,不同写法)
How Navicat imports MySQL scripts
杰理之如若需要大包发送,需要手机端修改 MTU【篇】
配置树莓派接入网络
Detailed explanation | detailed explanation of internal mechanism of industrial robot
Short video, more and more boring?
杰理之BLE【篇】
随机推荐
位运算异或
Crawling exercise: Notice of crawling Henan Agricultural University
多线程和并发编程(二)
Path analysis model
The best way to learn SEO: search engine
Detailed explanation | detailed explanation of internal mechanism of industrial robot
【线上问题处理】因代码造成mysql表死锁的问题,如何杀掉对应的进程
leetcode1020. 飞地的数量(中等)
Bloom taxonomy
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code
word设置目录
TS基础篇
The psychological process from autojs to ice fox intelligent assistance
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
【MySQL学习笔记32】mvcc
On the world of NDK (2)
Zhongqing reading news
Leetcode35. search the insertion position (simple, find the insertion position, different writing methods)
word怎么只删除英语保留汉语或删除汉语保留英文
Hydra common commands