View Full Version : Convert MyObject (base class is QObject) to QVariant... How to?

15th December 2009, 08:12

I need to convert my user type (MyObject, base class is QObject) to QVariant?

class MyObject : public QObject
public slots:

How convert MyObject to QVariant?


15th December 2009, 08:28
can we ask -- why?

15th December 2009, 08:46
anyway, the answer is no, because as Qt Assistants say for Q_DECLARE_METATYPE the following thing:

This macro makes the type Type known to QMetaType as long as it provides a public default constructor, a public copy constructor and a public destructor.

but QObject's copy constructor is private.

15th December 2009, 08:57
Yes, you can!

Tag: "using standart and user types in QtScript"

I has class, it has function that return several types (int, bool, string, and USER TYPES)
This class I want use in QtScript.

//this objects I use in QtScript!
class Document: public QObject
public slots:
QString getName(); //method for use in QtScript

//simple example, in real it's hardly
class Catalogue : public OQbject
QVariant getValue(QString name)
if(fname == "Document")
Document doc = new Document();

//try convert to QVariant
return QVariant(doc);
else if(fname == "Boolean")
return QVariant(true);
... //etc.

next part, QtScript

var cat = new Catalogue();

var doc = cat.getValue("Document");
var flag = cat.getValue("Boolean");

//doc - is not simple object!
//flag - is simple bool object!

var txt = doc.getName();

Function getValue(...) return several type, it's may be simple bool or int type or user type such as Document and other!

If all Qt types was inherited from one base class I have no problem, but ... (in c# all types (int, bool, user types) inherited from Object base class by default)

May be anyone have another decision of this problem?

Thanks for help.

15th December 2009, 09:13
QObject has only private copy constructor, but it must be public for Q_DECLARE_METATYPE.
in you example you use pointers.

15th December 2009, 09:26
you can keep pointers, but not objects.

15th December 2009, 10:03
>>you can keep pointers, but not objects.

pointers to void *?

but how it use in QtScript&

var param = cat.getValue(); //here may be int, may be user type

15th December 2009, 11:01
pointers to void, to QObject, to QWidget. take a look at QVariant ctor which takes pointer to void.