Results 1 to 6 of 6

Thread: Enums to QVariants and comparing those after the conversion.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 Re: Enums to QVariants and comparing those after the conversion.

    Quote Originally Posted by MarekR22 View Post
    Where is defined QVariant::data?
    In the QVariant exposed C++ API. However its a public method defined from the exposed header of QVariant. It allows me to construct a QVariant by giving it a pointer of my value which is internally copied. That's the only way i have to construct a QVariant which type will be my usertype.
    [EDIT]You were right MarekR22, i made a typo when posting my snippets... the code should have read:
    Qt Code:
    1. itemData(i)==v
    To copy to clipboard, switch view to plain text mode 
    My bad![/EDIT]

    Quote Originally Posted by MarekR22 View Post
    I don't see it in documentation!
    Aren't the public methods exposed in a header file the best documentation for a class?

    Quote Originally Posted by MarekR22 View Post
    I understand that you are assuming that nums are not flag kind (QMetaEnum::isFlag)!
    Every enum can be treated as int value! So you should compare enums as ints:
    [CODE]bool ok = false;
    if (itemData(i).toInt() ==v.toInt(&ok))
    The conversion isnt working. QVariant::toInt when storing enum values always return 0. i have no idea why. It seems that when constructing a QVariant from a usertype, i won't be able to convert them later.

    Quote Originally Posted by MarekR22 View Post
    Why did you chose QVariant::Type as constructor parameter?
    As i said: That's the only way my QVariant will then carry the right type...

    Quote Originally Posted by MarekR22 View Post
    Why not QVariant, enum name (const char *) or QMetaEnum?
    Your first suggestion constructs a QVariant based on QString, the problem is that the typing information is lost when doing that. Your second suggestion is a no-do. QMetaEnum is not actually carying the value of the enum, just its typing information...


    To summarize what i need: QVariant can carry both the typing and the value of things... i need to use both, i.e. to be able to first look at QVariant types (To know if they are storing enum values from the same enum), and them compare them.

    Any other idea please? An actual working sample code would greatly help...
    Pierre.
    Last edited by hickscorp; 8th August 2011 at 13:18.

  2. #2
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Enums to QVariants and comparing those after the conversion.

    Not sure I understand correctly what you need, but take a look at these functions:

    Qt Code:
    1. Type QVariant::type () const
    2. int QVariant::userType () const
    3. const char * QVariant::typeName () const
    4. const char * QVariant::typeToName ( Type typ ) [static]
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Enums to QVariants and comparing those after the conversion.

    Qt Code:
    1. ...
    2. for (quint32 i=0; i<cEnum; i++) {
    3. const char *key = en.key(i);
    4. uint const val = en.value(i);
    5. QVariant const var (t, &val);
    6. addItem (classNameToNaturalString(key), var);
    7. qDebug() << var;
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

    the address to the uint gets invalid after the foreach block.
    Last edited by nightghost; 8th August 2011 at 15:18. Reason: spelling corrections

Similar Threads

  1. How to use enums in qml
    By nightroad in forum Qt Quick
    Replies: 0
    Last Post: 29th June 2011, 13:24
  2. Best performance when comparing two QTextEdits
    By leenxkewl in forum Qt Programming
    Replies: 3
    Last Post: 16th October 2010, 19:07
  3. Comparing the |(shift+\) char
    By warry in forum Newbie
    Replies: 1
    Last Post: 8th September 2008, 07:05
  4. Comparing Items In List Box
    By kenny_isles in forum Qt Programming
    Replies: 9
    Last Post: 21st February 2007, 13:06
  5. how to use enums
    By soul_rebel in forum General Programming
    Replies: 3
    Last Post: 23rd March 2006, 21:49

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.