Enum to QString
-
Hello!
I'm trying to convert the name of any Enum instances (at least enums defined in the Qt framework) to QString.
For example: QAbstractSocket::TcpSocket, I would like a QString to have the value TcpSocket.Here is my method which only works for AbstractSocket::SocketType enum.
@#include <QCoreApplication>
#include <QString>
#include <QAbstractSocket>
#include <QMetaEnum>#include <iostream>
using namespace std;
QString EnumSocketTypeToString(int value)
{
QMetaObject obj = QAbstractSocket::staticMetaObject;
QMetaEnum en = obj.enumerator(0);
return QLatin1String(en.valueToKey(value));
}int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);QString type = EnumSocketTypeToString(QAbstractSocket::TcpSocket); cout << type.toStdString() << endl; return a.exec();
}@
The result is: TcpSocket, which is just what I want.
I then tried to modify the EnumSocketTypeToString() method to something like this:
@QString EnumToString(int value)
{
QMetaObject obj = QObject::staticMetaObject;
QMetaEnum en = obj.enumerator(0);
return QLatin1String(en.valueToKey(value));
}@And I tried this with the QSerialPort::Parity enum, the EvenParity instance.
@#include <QSerialPort>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);QString str = EnumToString(QSerialPort::EvenParity); cout << str.toStdString() << endl; return a.exec();
}@
This compiles but the returned QString from the method is empty.
I guess the:
@QMetaObject obj = QObject::staticMetaObject;@is the problem and does not work around for the QSerialPort class.
Any solution which works for any Qt defined Enums?
I'm in need of printing the names of an enum's instance in this fashion./Steffe
-
welcome to devnet
Did you test your modified routine for QAbstractSocket?
It should not work there either.
"staticMetaObject":http://qt-project.org/doc/qt-5/qobject.html#staticMetaObject-var is a static function of QObject. You call explicitely this static function while in the initial version with QAbstractSocket you are calling explicitly the version of QAbstractSocket.This may work:
@
QString EnumToString(QMetaObject & obj, int value)
{
QMetaEnum en = obj.enumerator(0);
return QLatin1String(en.valueToKey(value));
}#include <QSerialPort>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);QString str = EnumToString(QSerialPort::staticMetaObject, QSerialPort::EvenParity); cout << str.toStdString() << endl; return a.exec();
}
@Note: not tested. Brain to keyboard only :-)
-
Thanks for the reply koahnig!
Your solution almost works. The function declaration must be:
@QString EnumToString(QMetaObject obj, int value)@When trying QSerialPort::staticMetaObject with QSerialPort::EvenParity the result is: "Output".
When trying QAbstractSocket::staticMetaObject, QAbstractSocket:TcpSocket the result is "TcpSocket".
Fooling around with other enums within the QSerialPort class the result is either empty string or some other string.
QAbstractSocket::NetworkLayerProtocol enum doesn't work either. But I changed the index from 0 to 1 as:
@obj.enumerator(1);@which seem to have something to do with it. Therefor got it working for QAbstractSocket::NetworkLayerProtocol enum but SocketType enum doesn't work.
Any solution?
/Steffe
-
Why you have to generate a copy instead of just using the reference is not clear to me. However, the suggested code is based on simple cpp rules no magic.
For the different meaning of the member functions you need to consult the documentation. for the "enumerator":http://qt-project.org/doc/qt-5/qmetaobject.html#enumerator and for the "QMetaEnum":http://qt-project.org/doc/qt-5/QMetaEnum.html
My wild guess would that it has something to do the offset and count. However, just guessing.