Results 1 to 8 of 8

Thread: problem with qvariant_casting to a usertype

  1. #1

    Default problem with qvariant_casting to a usertype

    Hi I have the following and I guess very stupid questions:

    I have defined a usertype which I store in a qvariant. In my app I later try to get the data (which is a class pointer) out of the qvariant via
    Qt Code:
    1. myVariant.value<MyClass *>();
    To copy to clipboard, switch view to plain text mode 

    But the returned value is a zero pointer. I have tracked the problem down to this code in qvariant.h(line 564 qt4.5.0):
    Qt Code:
    1. template<typename T> T qvariant_cast(const QVariant &v)
    2. {
    3. const int vid = qMetaTypeId<T>(static_cast<T *>(0)); // returns 257 which is the typeid of MyClass*
    4. if (vid == v.userType()) //PROBLEM: v.userType returns 256
    5. return *reinterpret_cast<const T *>(v.constData()); // wont get executed...but should be
    6. if (vid < int(QMetaType::User)) {
    7. T t;
    8. if (qvariant_cast_helper(v, QVariant::Type(vid), &t))
    9. return t;
    10. }
    11. return T(); // zero pointer (in debug mode)
    12. }
    To copy to clipboard, switch view to plain text mode 

    The function compars the typeid of my userdata which is 257 with the userType value returned from v. Only when they are the same the function actually casts the constData into MyClass*. But when one has more Userdata types (than one) those all get an id bigger than 256 which is the base id of all userdata type ids. So this comparison seems quite weird to me.

    I know that the qtcode is working as it should and therefore I guess I have some big missunderstanding of userdata type ids and their usage. Can someone help me and shed some light on how I get my userdata casted correctly?

    Many thanks in advance,
    David

    P.S.: Hi All, great forum!

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem with qvariant_casting to a usertype

    please show us your code where you declare your custom variant type and where you use it

  3. #3

    Default Re: problem with qvariant_casting to a usertype

    Hi caduel,

    I do it quite the usual way I guess. ViewStateInfo derives from QObject somewhere up in the inheritance chain. Thanks for taking a look mate.

    So this is the header of the usertype.
    Qt Code:
    1. #pragma once
    2. #include "ViewStateInfo.hpp"
    3.  
    4.  
    5.  
    6. namespace composer
    7. {
    8. namespace model
    9. {
    10. //
    11. //
    12. //
    13. class LabelStateInfo : public ViewStateInfo
    14. {
    15. public:
    16.  
    17. LabelStateInfo(); // constructor
    18.  
    19.  
    20. // [...]
    21.  
    22. private:
    23. };
    24. }
    25. }
    26. Q_DECLARE_METATYPE(composer::model::LabelStateInfo*)
    To copy to clipboard, switch view to plain text mode 

    David

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with qvariant_casting to a usertype

    QVariant can accept pointers to QObject right out of the box so you don't have to declare the metatype for your own class - you can store it normally and then after retrieving it, cast it to proper type.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5

    Default Re: problem with qvariant_casting to a usertype

    Hi Wysota, thanks for your reply.

    I will try what you proposed once Im at home. I didnt know that and it sounds like a good idea especially because I have a templated wrapper to the qvariant casting in my app already.

    Out of curiosity: In theory what I was trying to do should work, shouldnt it?

    Cheers,
    David

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with qvariant_casting to a usertype

    Yes, but without seeing the actual code it is hard to say what you did wrong.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7

    Default Re: problem with qvariant_casting to a usertype

    Hi, sorry but I like to bring this up again (as I hate unsolved problems and the proposed workaround of using QObjects would pose some nasty additional expenses on client code side):

    The thing is, that the qmetatype registration obviously seems to work because my type has an id(257) which is successfully retrieved within qvariant_cast. So if you register a couple of usertypes shouldnt they get all an id from 256+ ?

    I try to find out whether the usertype registration is working or not. And from what I see it is working. And if it is working then there really isnt anything else I can do wrong, isnt there?

    David

    P.S.: does somebody understand the == comarison in line 4 of the initial code snippet and explain it to me? I would expect it to be >= since there isnt only one usertype of id 256 qvariant_cast may cast to, but there are any numbers 256+

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with qvariant_casting to a usertype

    Can you show us the exact code you use?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. deployment problem: msvc++ 2008 Express, Qt 4.4.3
    By vonCZ in forum Qt Programming
    Replies: 7
    Last Post: 10th November 2008, 14:38
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  3. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  4. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Tags for this Thread

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.