[Interest] C++11 enum class and QSettings

Bob Hood bhood2 at comcast.net
Mon Dec 5 04:15:42 CET 2016


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

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?



More information about the Interest mailing list