Results 1 to 8 of 8

Thread: Convert MyObject (base class is QObject) to QVariant... How to?

  1. #1
    Join Date
    Oct 2009
    Location
    Russia, South Ural, Chelyabinsk
    Posts
    42
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Convert MyObject (base class is QObject) to QVariant... How to?

    Hi!

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

    class MyObject : public QObject
    {
    Q_OBJECT
    public slots:
    ...
    };

    How convert MyObject to QVariant?

    Thanks.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Convert MyObject (base class is QObject) to QVariant... How to?

    can we ask -- why?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Convert MyObject (base class is QObject) to QVariant... How to?

    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    Oct 2009
    Location
    Russia, South Ural, Chelyabinsk
    Posts
    42
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Convert MyObject (base class is QObject) to QVariant... How to?

    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
    {
    Q_OBJECT
    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.
    Last edited by sergey_85; 15th December 2009 at 08:15.

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Convert MyObject (base class is QObject) to QVariant... How to?

    QObject has only private copy constructor, but it must be public for Q_DECLARE_METATYPE.
    in you example you use pointers.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Convert MyObject (base class is QObject) to QVariant... How to?

    you can keep pointers, but not objects.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Oct 2009
    Location
    Russia, South Ural, Chelyabinsk
    Posts
    42
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Convert MyObject (base class is QObject) to QVariant... How to?

    >>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

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Convert MyObject (base class is QObject) to QVariant... How to?

    pointers to void, to QObject, to QWidget. take a look at QVariant ctor which takes pointer to void.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Convert between a custom data type wrapped in a QVariant
    By darkadept in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 09:07
  2. Replies: 3
    Last Post: 27th December 2008, 19:34
  3. MVC - Abstract Widget Base Class - setupUI
    By SenSej in forum Newbie
    Replies: 0
    Last Post: 13th October 2008, 10:44
  4. Replies: 4
    Last Post: 19th March 2008, 17:47
  5. Connecting to a base class signal?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 22:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.