Qt4 QSettings保存枚举值(例如Qt :: CheckState)

编程入门 行业动态 更新时间:2024-10-23 05:47:34
本文介绍了Qt4 QSettings保存枚举值(例如Qt :: CheckState)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在QSetting中保存QCheckBok的状态,我可以将其值强制转换为int,但也许存在更简单,更合适的方法吗?

i wanna save state of QCheckBok in QSetting, i can cast its value to int but maybe exists more simple and proper method to do it?

这是我的代码:

QSetting setting; Qt::CheckState checkState; //... checkState = (Qt::CheckState)setting.value("checkState", Qt::Unchecked).toUInt(); //... setting.setValue("checkState", (uint)checkState); setting.sync();

推荐答案

首先,尝试避免使用C样式强制转换.例如,替换以下行:

Firstly, try to avoid C-style casts. For example, replace the following line:

checkState = (Qt::CheckState)setting.value("checkState", Qt::Unchecked).toUInt();

与此:

checkState = static_cast<Qt::CheckState>(setting.value("checkState", Qt::Unchecked).toUint());

将checkState转换为uint的行也应更改.

The line where you cast checkState to a uint should also be changed.

第二,QSettings依赖QVariant来设置和检索值.通常可以使用Q_DECLARE_METATYPE宏将QVariant扩展为支持其他类型.这是文档:

Secondly, QSettings relies on QVariant for setting and retrieving values. QVariant can usually be expanded to support additional types using the Q_DECLARE_METATYPE macro. Here's the documentation:

doc.trolltech/4.6/qmetatype.html#Q_DECLARE_METATYPE

但是,此机制似乎不适用于枚举(当您在QVariant上调用value()成员函数时).因此,您现在所拥有的(减去C样式的强制转换)就可以了.

However, this mechanism does not appear to work properly with enumerations (when you call the value() member function on QVariant). So what you have right now (minus the C-style casting) is fine.

更多推荐

Qt4 QSettings保存枚举值(例如Qt :: CheckState)

本文发布于:2023-10-09 18:29:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476444.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:QSettings   CheckState   Qt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!