我正在尝试使用 QSettings 在我的 QT 应用程序中保存一些设置。为此,我定义了一个类型:
typedef QMap
<
QString,
QMap<QString, QVariant>
>
QSession;
然后我就注册了
Q_DECLARE_METATYPE(QSession);
创建一些条目并在运行时读取它们没有问题,但是我在关闭应用程序时遇到以下错误
QVariant::save: unable to save type 'QSession' (type id: 1067).
我的保存功能看起来像那样
void saveSession()
{
QSession session;
for(auto it = pool.begin(); it != pool.end(); ++it)
{
QString hash(toQString((*it).getHash()));
session[hash]["name"] = toQVariant(toQString((*it).getName()));
session[hash]["size"] = toQVariant((*it).getSize());
session[hash]["timeout"] = toQVariant((*it).getTimeout());
}
this->settings.setValue("session", QVariant::fromValue(session));
}
问题出在哪里?谢谢!
最佳答案
您必须为您的 QSession
类型注册流式操作符 - 参见 qRegisterMetaTypeStreamOperators .
关于c++ - 如何将 QMap 存储到 QSetting 变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35803615/