可以使用QSETTINGS和自定义类型获得正确的值

发布于 2025-02-08 07:15:51 字数 1375 浏览 2 评论 0原文

我正在尝试使用QSETTINGS从INI文件中读取一个小结构。写作效果很好,但是当我尝试阅读它时,我总是从Qvariant中获得默认值。

这是结构定义:

struct CellSize {
    int font;
    int cell;
};

inline QDataStream& operator<<(QDataStream& out, const CharTableView::CellSize& v) {
    out << v.font << v.cell;
    return out;
}

inline QDataStream& operator>>(QDataStream& in, CharTableView::CellSize& v) {
    int font, cell;
    in >> font >> cell;
    v.font = font;
    v.cell = cell;
    return in;
}

inline bool operator==(const CharTableView::CellSize& a, const CharTableView::CellSize& b) {
    return a.font == b.font && a.cell == b.cell;
}

Q_DECLARE_METATYPE(CharTableView::CellSize)

我正在使用m_settings.setValue(设置:: tablecellsize,qvariant :: frofValue&lt; chartableview :: celtableview :: Cellize&gt;(ChartableView :: CellableView :: CellizeSMALL));。我认为这很好,因为INI文件中的Gibberish与UI更改一致。我的阅读代码是ChartableView :: Cellize tableCellSize = m_settings.value(设置:: tablecellsize).value&lt; chartableView :: cellize&gt;();();,它始终给我{0,0,0,0,0,0,0,0,0,0,0,0}

我是QT的新手,老实说,我对Qvariant和所有元编程的东西感到困惑。我想念什么吗?

编辑:我试图设置一些断点。基本上,触发的第一个断点是我从QSETTINGS读取值的值之后,这是{0,0,0}一如既往。然后,一段时间后,运算符内部的断点&gt;被触发,并且在操作员函数中font and cell 的值正确。发生了什么事?

I am trying to read a small struct from my ini file using QSettings. Writing works fine, but when I try to read it back I always get the default value out of QVariant.

This is the structure definition:

struct CellSize {
    int font;
    int cell;
};

inline QDataStream& operator<<(QDataStream& out, const CharTableView::CellSize& v) {
    out << v.font << v.cell;
    return out;
}

inline QDataStream& operator>>(QDataStream& in, CharTableView::CellSize& v) {
    int font, cell;
    in >> font >> cell;
    v.font = font;
    v.cell = cell;
    return in;
}

inline bool operator==(const CharTableView::CellSize& a, const CharTableView::CellSize& b) {
    return a.font == b.font && a.cell == b.cell;
}

Q_DECLARE_METATYPE(CharTableView::CellSize)

I am writing with m_settings.setValue(Settings::TableCellSize, QVariant::fromValue<CharTableView::CellSize>(CharTableView::CellSizeSmall));. I assume this is working fine because the gibberish inside the ini file is consistent with the UI changes. My reading code is CharTableView::CellSize tableCellSize = m_settings.value(Settings::TableCellSize).value<CharTableView::CellSize>(); and it always gives me {0, 0}.

I'm fairly new to Qt and, to be honest, I am a litte confused by QVariant and all the metaprogramming stuff. Am I missing something?

EDIT: I have tried to set some breakpoints. Basically, the first breakpoint that gets triggered is the one after I've read the value from QSettings, which is {0, 0} as always. Then after a while a breakpoint inside operator>> gets triggered and the values of font and cell inside the operator function are correct. What's happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑饮青盏花 2025-02-15 07:15:51

我解决了这个问题!我必须添加qregistermetatype&lt; ChartableView :: Cellize&gt;()内部ChartableView构造器。

I solved the issue! I had to add qRegisterMetaType<CharTableView::CellSize>() inside CharTableView constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文