[Resolved] How to use ENUM?
-
Well, this is a very basic question...
I have A enum, like:@
enum DataBitsType
{
DATA_5 = 5,
DATA_6 = 6,
DATA_7 = 7,
DATA_8 = 8
};
@I'm trying to show the values of this enum on ComboBox, but I don't know how to do that.
I try "ui->cbTaxaTransmissao->addItem(DATA_5);" but I got this error: "No matching function for call to 'QComboBox::addItem(DataBitsType)'"...
There is a simple way to do what I trying to do?!THanks all!!
-
addItem() takes a QString, so you will have to convert your param by e.g. QString::number(DATA_5) or QLocale().toString(DATA_5).
-
I found something about meta-objects:
http://srikanthsombhatla.wordpress.com/2010/08/13/convert-enum-to-qstring/
-
Well in general you can. You would have to register your enum with Qt meta-object system.
Take a look here for example: http://doc.qt.digia.com/qt/qobject.html#Q_ENUMS
Then you could use QMetaEnum class to find that type and enumerate all values with methods keyCount() and value(index).In simple cases with continuous enums its often easier to just define two more enum values like this @enum DataBitsType
{
DATA_5 = 5,
DATA_6 = 6,
DATA_7 = 7,
DATA_8 = 8,DATA_START = DATA_5, DATA_END = DATA_8
};@
and then do a simple loop from DATA_START to DATA_END.
-
Well I have more complex Enum:
@
enum BaudRateType
{
#if defined(Q_OS_UNIX) || defined(qdoc)
BAUD50 = 50, //POSIX ONLY
BAUD75 = 75, //POSIX ONLY
BAUD134 = 134, //POSIX ONLY
BAUD150 = 150, //POSIX ONLY
BAUD200 = 200, //POSIX ONLY
BAUD1800 = 1800, //POSIX ONLYif defined(B76800) || defined(qdoc)
BAUD76800 = 76800, //POSIX ONLY
endif
if (defined(B230400) && defined(B4000000)) || defined(qdoc)
BAUD230400 = 230400, //POSIX ONLY BAUD460800 = 460800, //POSIX ONLY BAUD500000 = 500000, //POSIX ONLY BAUD576000 = 576000, //POSIX ONLY BAUD921600 = 921600, //POSIX ONLY BAUD1000000 = 1000000, //POSIX ONLY BAUD1152000 = 1152000, //POSIX ONLY BAUD1500000 = 1500000, //POSIX ONLY BAUD2000000 = 2000000, //POSIX ONLY BAUD2500000 = 2500000, //POSIX ONLY BAUD3000000 = 3000000, //POSIX ONLY BAUD3500000 = 3500000, //POSIX ONLY BAUD4000000 = 4000000, //POSIX ONLY
endif
#endif //Q_OS_UNIX
#if defined(Q_OS_WIN) || defined(qdoc)
BAUD14400 = 14400, //WINDOWS ONLY
BAUD56000 = 56000, //WINDOWS ONLY
BAUD128000 = 128000, //WINDOWS ONLY
BAUD256000 = 256000, //WINDOWS ONLY
#endif //Q_OS_WIN
BAUD110 = 110,
BAUD300 = 300,
BAUD600 = 600,
BAUD1200 = 1200,
BAUD2400 = 2400,
BAUD4800 = 4800,
BAUD9600 = 9600,
BAUD19200 = 19200,
BAUD38400 = 38400,
BAUD57600 = 57600,
BAUD115200 = 115200
};
@ -
So in your case meta-object system is the way to go.
-
Hi, I find this line in the QVariant class documentation :
bq. QVariant can be extended to support other types than those mentioned in the Type enum. See the QMetaType documentation for details.
I think the idea is to use QMetaType !
So you have to use the method :
@void QComboBox::addItem ( const QString & text, const QVariant & userData = QVariant() )@But before you have to declare a new type using the macros :
@ Q_DECLARE_METATYPE(DataBitsType)@
-
issam - QVariant and Q_DECLARE_META_TYPE have nothing to do here. the userData param in QComboBox::addItem() is just for that - user data. It doesn't show in combo as a string.
dcbasso - the complete example of what you try to do is here:
myclass.h @#include <QObject>
class MyClass : public QObject {
Q_OBJECT
Q_ENUMS(MyEnumType)
public:
enum MyEnumType { Value1 = 24, Value2 = 42 };
};@and with that you can do: @#include "myclass.h"
#include <QApplication>
#include <QComboBox>
#include <QMetaObject>
#include <QMetaEnum>int main(int argc, char *argv[])
{
QApplication a(argc, argv);MyClass myObj; const QMetaObject* metaObj = myObj.metaObject(); QMetaEnum enumType = metaObj->enumerator(metaObj->indexOfEnumerator("MyEnumType")); QComboBox combo; for(int i=0; i < enumType.keyCount(); ++i) { QString item = QString::fromAscii(enumType.key(i)) + " is " + QString::number(enumType.value(i)); combo.addItem(item); } combo.show(); return a.exec();
}@