Results 1 to 6 of 6

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

Threaded 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 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.

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
  •  
Qt is a trademark of The Qt Company.