我想在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)
发布评论