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

末鹿安然 提交于 2019-12-10 17:45:26

问题


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::CheckState)setting.value("checkState", Qt::Unchecked).toUInt();
//...
setting.setValue("checkState", (uint)checkState);
setting.sync();

回答1:


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.



来源:https://stackoverflow.com/questions/2102971/qt4-qsettings-save-enumeration-value-for-example-qtcheckstate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!