2

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:

WgtEnumItemEditor::WgtEnumItemEditor (QVariant::Type t, QWidget *p) : QComboBox(p) {
    QMetaObject const &mo = staticMetaObject;
    QString mtName = QMetaType::typeName(t);
    mtName = mtName.mid( mtName.lastIndexOf(':')+1 );
    qint32 const iEnum = mo.indexOfEnumerator( mtName.toLatin1() );
    QMetaEnum const &en = mo.enumerator( iEnum );
    quint32 const cEnum = en.keyCount();

    for (quint32 i=0; i<cEnum; i++){
        const char *key = en.key(i);
        uint const val = en.value(i);
        QVariant const var ( t, &val );
        addItem( classNameToNaturalString(key), var );
        qDebug() << var;
    }
}

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:

void WgtEnumItemEditor::setValue (const QVariant &v) {
    for (quint32 i=0; i<count(); i++) {
        if (itemData(i)==v.data) {
            setCurrentIndex(i);
            break;
        }
    }
}

This piece of code doesn't work. i have to replace the test by:

*(uint*)itemData(i).data()==*(uint*)v.data()

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 empty values outputs. For instance, the output generated in the first chunk of code looks like this:

QVariant(ReadingDirection, ) 
QVariant(ReadingDirection, )
QVariant(ReadingDirection, )
QVariant(ReadingDirection, )
QVariant(ReadingDirection, )
QVariant(BarcodeStandard, )
QVariant(BarcodeStandard, )
QVariant(BarcodeStandard, )

As you can see, the value isn't outputed correctly... Can you please help me find why?

Thank you, Pierre.

Exa
  • 4,020
  • 7
  • 43
  • 60
Doodloo
  • 869
  • 5
  • 18
  • In `QVariant const var (t, &val);` you are taking the address of a local variable. I don't think you should do that. Have you tried `QVariant const var (t, val);` instead? Then v.data() will be an integer and not a pointer (which actually points at ... who knows?!) – Omri Barel Aug 07 '11 at 18:06
  • This is obviously wrong for 2 reasons: 1) This specific constructor takes a first parameter for the type, and a second for a pointer which *value* is then internally copied to another internal memory location. The QVariant never directly store the address pointed by the second parameter, instead it takes what is stored at this address and copies it. 2) When then using v.data(), it is giving me a void* (Or when using constData, a const void*). This is actually giving me the int value of the enum when using it like this: *(const int*)*v.constData(), which is working perfectly! – Doodloo Aug 08 '11 at 11:38
  • Just for clarification: My solution is working, but not clean. Also, i'd like to have a solution outputing the right values to qDebug(), while being able to use QVariant carrying the type and value information of my data. – Doodloo Aug 08 '11 at 11:41

1 Answers1

0

I am not sure if this solves your Problem, but to use a custom type as QVariant you need to use the Q_DECLARE_METATYPE Makro. See here: http://doc.qt.digia.com/qt/qmetatype.html#Q_DECLARE_METATYPE

TWE
  • 431
  • 2
  • 11