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:
mtName = mtName.mid(mtName.lastIndexOf(':')+1);
qint32 const iEnum = mo.indexOfEnumerator(mtName.toLatin1());
quint32 const cEnum = en.keyCount();
for (quint32 i=0; i<cEnum; i++) {
const char *key = en.key(i);
uint const val = en.value(i);
addItem (classNameToNaturalString(key), var);
qDebug() << var;
}
connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(this_currentIndexChanged(int)));
}
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;
}
connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(this_currentIndexChanged(int)));
}
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:
void WgtEnumItemEditor
::setValue (const QVariant &v
) { for (quint32 i=0; i<count(); i++) {
if (itemData(i)==v.data) {
setCurrentIndex(i);
break;
}
}
}
void WgtEnumItemEditor::setValue (const QVariant &v) {
for (quint32 i=0; i<count(); i++) {
if (itemData(i)==v.data) {
setCurrentIndex(i);
break;
}
}
}
To copy to clipboard, switch view to plain text mode
This piece of code doesn't work. i have to replace the test by:
*(uint*)itemData(i).data()==*(uint*)v.data()
*(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:
QVariant(ReadingDirection, )
QVariant(ReadingDirection, )
QVariant(ReadingDirection, )
QVariant(ReadingDirection, )
QVariant(ReadingDirection, )
QVariant(BarcodeStandard, )
QVariant(BarcodeStandard, )
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.
Bookmarks