开发者

Qt4 QSettings save enumeration value (for example Qt::CheckState)

开发者 https://www.devze.com 2022-12-18 01:59 出处:网络
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?

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?

here is my code:

QSetting setting;
Qt::CheckState checkState;
//...
checkState = (Qt::CheckSta开发者_运维百科te)setting.value("checkState", Qt::Unchecked).toUInt();
//...
setting.setValue("checkState", (uint)checkState);
setting.sync();


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

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

with this:

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

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

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:

http://doc.trolltech.com/4.6/qmetatype.html#Q_DECLARE_METATYPE

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消