Solved Casting a custom class to QVariant (2020 but still relevant)
-
Hi
Yes https://doc.qt.io/qt-5/qtqml-cppintegration-data.html mentions
QVariantMap so that should work.What error did you get with QVariantMap ?
Also you must also use pointers to "cMyClass"
And it should be supported
https://doc.qt.io/qt-5/qmetatype.html#Type-enum
as it has QMetaType::QObjectStar -
@mrjj The problem is getting myClass into the QVariant. Using the following required me to use Q_DECLARE_METATYPE.
QVariant v = QVariant::fromValue<cMyClass>(MyObject);
However, as soon as I use Q_DECLARE_METATYPE, I get errors about using a deleted function. Without this reference, it compiles just fine. But I can't see a method to insert myClass into QVariantMap without using QVariant::fromValue.
-
Again: you can't copy a QObject! use a pointer...
-
@Christian-Ehrlicher There's something I'm not understanding here. Here's my class. When I uncomment the part about Q_DECLARE_METATYPE, I immediately get the compiler error with the use of deleted function.
#include <QObject>
// #include <QMetaType>
#include "PropertyHelper.h"class cMyClass: public QObject
{
Q_OBJECT
AUTO_PROPERTY(QString, nodeValue)
AUTO_PROPERTY(QString, nodeUnits)
AUTO_PROPERTY(QStringList, nodeList)
AUTO_PROPERTY(qint16, currentIndex)
public:
explicit cMyClass(QObject* parent = nullptr);
explicit cMyClass(QObject* parent, QString controlName, QString value, std::vector<QString> nodeStringVector);
QString controlName() const {return mControlName;}private:
QString mControlName;
};// Q_DECLARE_METATYPE(cMyClass);
In my ViewMgr class, I used this as my Q_PROPERTY.
Q_PROPERTY(QVariantMap *myObject READ myObject WRITE setMyObject NOTIFY myObjectChanged)
I guess I'm not sure where I should be using a pointer that I'm not.
-
@Doug-Beck said in Casting a custom class to QVariant (2020 but still relevant):
QVariant v = QVariant::fromValue<cMyClass>(MyObject);
here
-
@Christian-Ehrlicher Still get the same errors even without that line of code.
Start of compiler errors*
error: C2280: 'cMyClass::cMyClass:(const cMyClass: &)': attempting to reference a deleted function
compiler has generated 'cMyClass::cMyClass' here
while compiling class template member function 'void *QtMetaTypePrivate::QMetaTypeFunctionHelper<T,true>::Construct(void *,const void *)'
with
[
T=cMyClass
]see reference to function template instantiation 'void *QtMetaTypePrivate::QMetaTypeFunctionHelper<T,true>::Construct(void *,const void *)' being compiled
with
[
T=cMyClass
]see reference to class template instantiation 'QtMetaTypePrivate::QMetaTypeFunctionHelper<T,true>' being compiled
with
[
T=cFakeControl
]
End of compiler errors*I sincerely appreciate your help on this! I have spent much of the last decade using C# so it would not be surprising if I didn't use a pointer correctly. But I'm not sure where the problem is.
-
I don't know what else I can tell. You can't put a QObject into a QVariant, only a pointer to a QObject. Therefore pass a pointer ... c++ basics.
-
@Christian-Ehrlicher I get the part about pointers. However, when I copy cMyClass to an empty QML project and don't reference it at all and even remove all of the Q_PROPERTIES from cMyClass I still get the same error as before.
This new project does not use QVariant at all. Nor does it ever refer to cMyClass, which is just an empty class. But, the line Q_DECLARE_METATYPE does cause compiler errors referring to a deleted function. (Which admittedly is just like I'd have if I was trying to use QObject instead of QObject*). So, this has nothing to do QVariant or even my use of cMyClass since I'm not using it at all.
I hope you are saying that Q_DECLARE_METATYPE is completely unnecessary for the use of QVariant.
#include <QObject>
#include <QMetaType>class cMyClass: public QObject
{
Q_OBJECT
public:
explicit cMyClass(QObject* parent = nullptr);
};Q_DECLARE_METATYPE(cMyClass);
** Still happens
'cMyClass::cMyClass(const cMyClass &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'QObject::QObject(const QObject &)' -
@Doug-Beck said in Casting a custom class to QVariant (2020 but still relevant):
Q_DECLARE_METATYPE
Hi
https://doc.qt.io/qt-5/qmetatype.html#details
"Any class or struct that has a public default constructor, a public copy constructor, and a public destructor can be registered."
Which means you cant use
Q_DECLARE_METATYPE(cMyClass);Also I think QObject * is already registered.
-
@mrjj Thanks! Clearly that was a dead end. I think I understand what Christian-Ehrlicher was telling me.