Results 1 to 6 of 6

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

  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 Enums to QVariants and comparing those after the conversion.

    Hello,

    i am registering enums to the Qt meta-object system. i have double checked that all of those have a meta-type ID, and everything looks perfect. But i have some kind of unresolved issue with the comparison of those.

    Let's consider this code for populating a combobox:
    Qt Code:
    1. WgtEnumItemEditor::WgtEnumItemEditor (QVariant::Type t, QWidget *p)
    2. : QComboBox(p) {
    3. QMetaObject const &mo = staticMetaObject;
    4. QString mtName = QMetaType::typeName(t);
    5. mtName = mtName.mid(mtName.lastIndexOf(':')+1);
    6. qint32 const iEnum = mo.indexOfEnumerator(mtName.toLatin1());
    7. QMetaEnum const &en = mo.enumerator(iEnum);
    8. quint32 const cEnum = en.keyCount();
    9. for (quint32 i=0; i<cEnum; i++) {
    10. const char *key = en.key(i);
    11. uint const val = en.value(i);
    12. QVariant const var (t, &val);
    13. addItem (classNameToNaturalString(key), var);
    14. qDebug() << var;
    15. }
    16. connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(this_currentIndexChanged(int)));
    17. }
    To copy to clipboard, switch view to plain text mode 
    It works good, my list gets populated with human readable texts for my enums.

    Now, i'm trying to set the initial value displayed by the list with a QVariant itself containing a value for the enum:
    Qt Code:
    1. void WgtEnumItemEditor::setValue (const QVariant &v) {
    2. for (quint32 i=0; i<count(); i++) {
    3. if (itemData(i)==v.data) {
    4. setCurrentIndex(i);
    5. break;
    6. }
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    This piece of code doesn't work. i have to replace the test by:
    Qt Code:
    1. *(uint*)itemData(i).data()==*(uint*)v.data()
    To copy to clipboard, switch view to plain text mode 
    This way it works.

    Can anyone explain me how to enable comparison of my custom enums in QVariants? Or am i doing something wrong while creating the QVariant being used as userdata for the list maybe?

    Also, if i try to qDebug() the QVariants created in loop for populating the list, i get outputs like this:
    Qt Code:
    1. QVariant(ReadingDirection, )
    2. QVariant(ReadingDirection, )
    3. QVariant(ReadingDirection, )
    4. QVariant(ReadingDirection, )
    5. QVariant(ReadingDirection, )
    6. QVariant(BarcodeStandard, )
    7. QVariant(BarcodeStandard, )
    8. QVariant(BarcodeStandard, )
    To copy to clipboard, switch view to plain text mode 
    As you can see, the value isn't outputed correctly... Can you please help me find why?

    Thanks,
    Pierre.



    Thank you,
    Pierre.
    Last edited by hickscorp; 7th August 2011 at 16:41.

  2. #2
    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: Enums to QVariants and comparing those after the conversion.

    Where is defined QVariant::data? I don't see it in documentation!
    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:
    Qt Code:
    1. bool ok = flase;
    2. if (itemData(i).toInt() ==v.toInt(&ok))
    3. if (ok) {
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 


    Why did you chose QVariant::Type as constructor parameter? Why not QVariant, enum name (const char *) or QMetaEnum?

  3. #3
    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.

  4. #4
    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 

  5. #5
    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

  6. #6
    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.

    @nightghost: And i don't see the problem. When constructing the QVariant this way, the *pointer* isnt stored. Instead, the data it points to is copied. My thing is working, im asking for something else.


    Added after 6 minutes:


    Quote Originally Posted by mentalmushroom View Post
    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 
    Sorry but i really don't see how it can be helpfull to me.

    Thread simplified and moved here: http://www.qtcentre.org/threads/4375...-and-QVariants.
    Last edited by hickscorp; 8th August 2011 at 19:40.

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.