From 19c5b80b24c8c9d7a68b406edf24940ea38be755 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 9 Feb 2023 16:43:58 +0200 Subject: [PATCH] QVariant::value() fails to convert unless QVariant::fromValue() has been called previously. --- src/app/puzzle/vpsettings.cpp | 5 +++-- src/app/valentina/main.cpp | 4 ++-- src/libs/vmisc/compatibility.h | 18 ++++++++++++++++++ src/libs/vmisc/testvapplication.h | 3 ++- src/libs/vmisc/vvalentinasettings.cpp | 9 +++++---- src/libs/vmisc/vvalentinasettings.h | 2 -- 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/app/puzzle/vpsettings.cpp b/src/app/puzzle/vpsettings.cpp index 991f2d515..7aa6a2486 100644 --- a/src/app/puzzle/vpsettings.cpp +++ b/src/app/puzzle/vpsettings.cpp @@ -26,6 +26,7 @@ ** *************************************************************************/ #include "vpsettings.h" +#include "../vmisc/compatibility.h" #include @@ -66,14 +67,14 @@ VPSettings::VPSettings(Format format, Scope scope, const QString &organization, QObject *parent) : VCommonSettings(format, scope, organization, application, parent) { - qRegisterMetaTypeStreamOperators("QMarginsF"); + REGISTER_META_TYPE_STREAM_OPERATORS(QMarginsF) } //--------------------------------------------------------------------------------------------------------------------- VPSettings::VPSettings(const QString &fileName, QSettings::Format format, QObject *parent) : VCommonSettings(fileName, format, parent) { - qRegisterMetaTypeStreamOperators("QMarginsF"); + REGISTER_META_TYPE_STREAM_OPERATORS(QMarginsF) } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/valentina/main.cpp b/src/app/valentina/main.cpp index 761bf9ba5..dd7743ac1 100644 --- a/src/app/valentina/main.cpp +++ b/src/app/valentina/main.cpp @@ -69,9 +69,9 @@ auto main(int argc, char *argv[]) -> int #endif // Need to internally move a node inside a piece main path - qRegisterMetaTypeStreamOperators("VPieceNode"); + REGISTER_META_TYPE_STREAM_OPERATORS(VPieceNode); // Need to internally move a node inside a custom seam allowance path - qRegisterMetaTypeStreamOperators("CustomSARecord"); + REGISTER_META_TYPE_STREAM_OPERATORS(CustomSARecord); #ifndef Q_OS_MAC // supports natively InitHighDpiScaling(argc, argv); diff --git a/src/libs/vmisc/compatibility.h b/src/libs/vmisc/compatibility.h index 1f0f7c1b2..e9c132b81 100644 --- a/src/libs/vmisc/compatibility.h +++ b/src/libs/vmisc/compatibility.h @@ -34,6 +34,24 @@ #include #include +#include "defglobal.h" + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +// WARNING:QVariant::load: unknown user type with name QMarginsF. +// QVariant::value() fails to convert unless QVariant::fromValue() has been called previously. +// https://stackoverflow.com/questions/70974383/qvariantvaluet-fails-to-convert-unless-qvariantfromvaluet-has-been-c +#if QT_VERSION >= QT_VERSION_CHECK(6, 1, 0) +#define REGISTER_META_TYPE_STREAM_OPERATORS(TYPE) \ +QMetaType::fromType().hasRegisteredDataStreamOperators(); // Dummy call +#else +#define REGISTER_META_TYPE_STREAM_OPERATORS(TYPE) \ +QVariant::fromValue(TYPE{}); // Dummy call +#endif // QT_VERSION >= QT_VERSION_CHECK(6, 1, 0) +#else +#define REGISTER_META_TYPE_STREAM_OPERATORS(TYPE) \ +qRegisterMetaTypeStreamOperators(#TYPE); +#endif // QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + #if QT_VERSION < QT_VERSION_CHECK(5, 5, 0) #include "diagnostic.h" #endif // QT_VERSION < QT_VERSION_CHECK(5, 5, 0) diff --git a/src/libs/vmisc/testvapplication.h b/src/libs/vmisc/testvapplication.h index a50c14f50..bef581e75 100644 --- a/src/libs/vmisc/testvapplication.h +++ b/src/libs/vmisc/testvapplication.h @@ -32,6 +32,7 @@ #include "vabstractvalapplication.h" #include "projectversion.h" #include "../vmisc/vcommonsettings.h" +#include "../vmisc/compatibility.h" class VTestSettings : public VCommonSettings { @@ -41,7 +42,7 @@ public: QObject *parent = nullptr) : VCommonSettings(format, scope, organization, application, parent) { - qRegisterMetaTypeStreamOperators("QMarginsF"); + REGISTER_META_TYPE_STREAM_OPERATORS(QMarginsF); } }; diff --git a/src/libs/vmisc/vvalentinasettings.cpp b/src/libs/vmisc/vvalentinasettings.cpp index 96f4605d7..e1417468a 100644 --- a/src/libs/vmisc/vvalentinasettings.cpp +++ b/src/libs/vmisc/vvalentinasettings.cpp @@ -37,10 +37,11 @@ #include #include #include +#include +#include #include "../vmisc/def.h" -#include "../vmisc/vmath.h" -#include "../vlayout/vbank.h" +#include "../vmisc/compatibility.h" #include "qglobal.h" #ifndef QBS_BUILD @@ -147,14 +148,14 @@ VValentinaSettings::VValentinaSettings(Format format, Scope scope, const QString QObject *parent) :VCommonSettings(format, scope, organization, application, parent) { - qRegisterMetaTypeStreamOperators("QMarginsF"); + REGISTER_META_TYPE_STREAM_OPERATORS(QMarginsF); } //--------------------------------------------------------------------------------------------------------------------- VValentinaSettings::VValentinaSettings(const QString &fileName, QSettings::Format format, QObject *parent) :VCommonSettings(fileName, format, parent) { - qRegisterMetaTypeStreamOperators("QMarginsF"); + REGISTER_META_TYPE_STREAM_OPERATORS(QMarginsF); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vmisc/vvalentinasettings.h b/src/libs/vmisc/vvalentinasettings.h index ea6e984e5..c8c830c18 100644 --- a/src/libs/vmisc/vvalentinasettings.h +++ b/src/libs/vmisc/vvalentinasettings.h @@ -34,10 +34,8 @@ #include #include #include -#include #include "vcommonsettings.h" -#include "../vlayout/vbank.h" class VValentinaSettings : public VCommonSettings {