[Interest] C++11 enum class and QSettings

Ch'Gans chgans at gna.org
Mon Dec 5 04:51:16 CET 2016


On 5 December 2016 at 16:15, Bob Hood <bhood2 at comcast.net> wrote:
> I'm trying to save a C++11 enum class value in a QSettings.  In a header I
> have:
>
>     #include <QMetaType>
>
>     enum class ReportStacking
>     {
>         Stacked,
>         Intermixed
>     };
>     Q_DECLARE_METATYPE(ReportStacking)
>
> And then in the module, using a data member of this type:
>
>     ReportStacking report_stacking = ReportStacking::Intermixed;
>
> I've tried both:
>
>     QVariant var;
>     var.setValue(report_stacking);
>     settings.setValue("report.stacking", var);
>
> and:
>
>     settings.setValue("report.stacking",
> QVariant::fromValue(report_stacking));
>
> and I'm getting a run-time error:
>
>     QVariant::save: unable to save type 'ReportStacking' (type id: 1027).
>
>     ASSERT failure in QVariant::save: "Invalid type to save", file
> kernel\qvariant.cpp, line 2144
>     Debug Error!
>
>     Program: Q:\Qt\5.6.2\64_dll\bin\Qt5Cored.dll
>     Module: 5.6.2
>     File: global\qglobal.cpp
>     Line: 3026
>
>     ASSERT failure in QVariant::save: "Invalid type to save", file
> kernel\qvariant.cpp, line 2144

I might be wrong, but i think that Q_DECLARE_METATYPE doesn't cross
shared object boundary, if the code serialising the enum isn't in the
same shared object that the one that declare it, then you need to call
qRegisterMetaType() from your main object.

Chris


>
> Is there a Qt-base way of storing C++11 enum class types, or do I have to do
> some manual coding to map a ReportStacking class value to an int for
> QSettings to digest?
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest



More information about the Interest mailing list