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?
CodePudding user response:
I solved the issue! I had to add qRegisterMetaType<CharTableView::CellSize>()
inside CharTableView constructor.