Results 1 to 6 of 6

Thread: Simplified problem about enums and QVariants.

  1. #1
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Simplified problem about enums and QVariants.

    Ok i believe the problem i have posted before is too complicated to be read now. Lets make the question simple:
    Can someone provide me with an example of code showing how to construct a QVariant "v", based on a given QVariant::UserType "t" where "t" refers to a type registered for an enum type?
    Then when using qDebug() << v; i expect the type as well as the value to appear in stdout...

    i don't think i can go for a more simple question

    Thanks,
    Pierre.
    Last edited by hickscorp; 8th August 2011 at 19:42.

  2. #2
    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: Simplified problem about enums and QVariants.

    I don't know if that helps you in any way but here goes:
    Qt Code:
    1. class Cls : public QObject {
    2. Q_OBJECT
    3. public:
    4. enum X {
    5. A, B = 13, C
    6. }
    7. Q_ENUMS(X);
    8.  
    9. };
    10.  
    11. Q_DECLARE_METATYPE(Cls::X)
    12.  
    13. #include "main.moc"
    14.  
    15. int main(){
    16.  
    17. int xMT = qRegisterMetaType<Cls::X>("Cls::X");
    18. qDebug() << "Type id:" << xMT;
    19. QVariant v = qVariantFromValue(Cls::B);
    20. qDebug() << v;
    21. Cls::X x = v.value<Cls::X>();
    22. qDebug() << x;
    23. QMetaEnum en = Cls::staticMetaObject.enumerator(0);
    24. qDebug() << en.value(0) << en.value(1) << en.value(2);
    25. qDebug() << "Key for Cls::B is:" << en.valueToKey(Cls::B);
    26. qDebug() << "Key for v is:" << en.valueToKey(v.value<Cls::X>());
    27. qDebug() << "Key for v is:" << en.valueToKey(*(int*)v.data());
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 

    and the output:

    text Code:
    1. Type id: 258
    2. QVariant(Cls::X, )
    3. 13
    4. 0 13 14
    5. Key for Cls::B is: B
    6. Key for v is: B
    7. Key for v is: B
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 9th August 2011 at 07:39.
    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.


  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Simplified problem about enums and QVariants.

    @wysota: here is his initial thread. In short he want handle any kind of enums! His code doesn't know what enum will be used, only meta data for enum (name as C-string or QMetaEnum object) are available.

  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: Simplified problem about enums and QVariants.

    I know. The last version of code (casting to integer) will handle that case. There is not much magic in QVariant, at some point you need to know (in terms of symbols and not variables) what type to cast the data to. Since all enums are in fact integers, casting to an int should be fairly safe.
    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
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Simplified problem about enums and QVariants.

    @MarekR22: Exactly. In Wysota's code, the enum is template-constructed with the type Cls::X, which is not something i can do (Unless i make a branch of "if"s comparing metatype IDs and template constructing QVariants, but i thought this was already being generated at MOC'ing time).

    @Wysota: Thanks for the snippet, but you are constructing the QVariant with "qVariantFromValue(Cls::B);", and i was asking to be able to construct it with the type ID (In your snippet: the xMT variable is holding it).

    i guess i'm close to the solution since using the 3rd QVariant constructor makes the QVariant aware of the right type (In the QDebug, i get the right type as output), but i still get no value and using toInt won't work... However using a cast on the internal constData does, so i guess i'll have to stick to it...

    Quote Originally Posted by wysota View Post
    The last version of code (casting to integer) will handle that case.
    Exactly. That's what i'm doing already (See previous post here: http://www.qtcentre.org/threads/4372...the-conversion. ) but when qDebug()'ing those variants, the value doesn't appear...

    Thanks anyways, alot
    And if you know better or have any good idea for me, i'd be very gratefull at this point.
    Pierre.

  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: Simplified problem about enums and QVariants.

    Quote Originally Posted by hickscorp View Post
    @Wysota: Thanks for the snippet, but you are constructing the QVariant with "qVariantFromValue(Cls::B);", and i was asking to be able to construct it with the type ID (In your snippet: the xMT variable is holding it).
    It doesn't matter how you create the variant. You can see that the data is there as you can cast it back to the proper type.

    i guess i'm close to the solution since using the 3rd QVariant constructor makes the QVariant aware of the right type (In the QDebug, i get the right type as output)
    You can see I can get that without any problems as well.

    , but i still get no value and using toInt won't work...
    It won't work. Custom types are not castable to anything.

    but when qDebug()'ing those variants, the value doesn't appear...
    It won't appear because there are no operators for your custom enum type defined. QVariant simply can't handle this case and goes for a sane default, showing just the type and not the value. There is no relation between your custom type and the QMetaEnum for the class that encapsulates it so Qt can't handle it. It's possible you could try creating a class that is able to provide such relationship (and then resolve it when needed) and register that class with QVariant and then wrap your enum in this class but it's complicated and probably doesn't fit your usecase very well.

    And if you know better or have any good idea for me, i'd be very gratefull at this point.
    Stick with casting to int*, just make sure the compiler treats your enum as int and not short or something else as you might get weird results.
    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. Replies: 5
    Last Post: 8th August 2011, 19:41
  2. QString sth between trimmed and simplified
    By pmlody in forum Qt Programming
    Replies: 3
    Last Post: 3rd November 2009, 08:13
  3. Problems with QString::simplified() API
    By montylee in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2009, 04:14
  4. Replies: 1
    Last Post: 15th May 2009, 07:16
  5. How to disply Simplified Chinese in QTextBrowser
    By xjtu in forum Qt Programming
    Replies: 1
    Last Post: 21st April 2008, 08: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.