Results 1 to 4 of 4

Thread: Trouble with QVariant setValue() and Q_DECLARE_METATYPE

  1. #1
    Join Date
    May 2017
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Trouble with QVariant setValue() and Q_DECLARE_METATYPE

    I have a small class describing an object which I want to use in a QAbstractItemModel. It's not derived from anything, has a copy and a default constructor and at the end, I'm adding the Q_DECLARE_METATYPE macro which should - per Qt documentation - be sufficient to use the class as a QVariant.

    Qt Code:
    1. class Dataset
    2. {
    3. Dataset();
    4. Dataset(const Dataset& _data);
    5. ~Dataset();
    6. ...
    7. }
    8. Q_DECLARE_METATYPE(Dataset)
    To copy to clipboard, switch view to plain text mode 

    Here's how I use the class:

    Qt Code:
    1. ...
    2. QVariant var;
    3. Dataset data;
    4. var.setValue(data);
    5. ...
    To copy to clipboard, switch view to plain text mode 

    When trying to compile, I get the following output/error message:

    /usr/include/QtCore/qmetatype.h(169): error: class "QMetaTypeId<Dataset *>" has no member "qt_metatype_id"
    static inline int qt_metatype_id() { return QMetaTypeId<T>::qt_metatype_id(); }
    ^
    detected during:
    instantiation of "int QMetaTypeId2<T>::qt_metatype_id() [with T=Dataset *]" at line 230
    instantiation of "int qMetaTypeId(T *) [with T=Dataset *]" at line 463 of "/usr/include/QtCore/qvariant.h"
    instantiation of "void qVariantSetValue(QVariant &, const T &) [with T=Dataset *]" at line 528 of "/usr/include/QtCore/qvariant.h"
    instantiation of "void QVariant::setValue(const T &) [with T=Dataset *]" at line 21 of "/home/user/projects/playground/source/Shared/GUI/DatasetTreeModel.cpp"


    Any help is greatly appreciated!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Trouble with QVariant setValue() and Q_DECLARE_METATYPE

    If the class declaration code you have posted is your actual code, then none of the members shown are public (there is no "public:" section marker before the constructor). Thus, all of these members are private and inaccessible from outside the class.

    The example code you show also won't compile, since you can't create an instance of a class with a private constructor. Most likely, the compilation is failing when you #include the class header file, so it never gets to that point to complain about that.
    Last edited by d_stranz; 24th May 2017 at 23:51.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    May 2017
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trouble with QVariant setValue() and Q_DECLARE_METATYPE

    d_stranz, very true in pointing that out. I probably cut out too much of the class declaration. Of course, the constructors and the destructor are public and there are set/get routines for those members that are private. Any other idea, what might be the cause for the compile error? I've double and triple checked the code for any hint of problem and even reduced the code all the way down to the example given on the Qt website where they introduce this method.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Trouble with QVariant setValue() and Q_DECLARE_METATYPE

    No idea. If you're using Qt Creator, with a .pro file, try running qmake again.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QVariant::QVariant(Qt::GlobalColor)' is private
    By alenn.masic in forum Qt Programming
    Replies: 7
    Last Post: 22nd February 2013, 16:29
  2. Replies: 4
    Last Post: 4th January 2011, 12:07
  3. Q_DECLARE_METATYPE() questions.
    By hickscorp in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2010, 16:04
  4. How to use Q_DECLARE_METATYPE
    By lni in forum Qt Programming
    Replies: 6
    Last Post: 21st July 2008, 17:13
  5. Replies: 2
    Last Post: 12th December 2007, 13:15

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.