Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Enum to QString
Forum Updated to NodeBB v4.3 + New Features

Enum to QString

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 16.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mc_Topaz
    wrote on last edited by
    #1

    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&#40;&#41;;
    

    }@

    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&#40;&#41;;
    

    }@

    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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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&#40;&#41;;
      

      }
      @

      Note: not tested. Brain to keyboard only :-)

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mc_Topaz
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          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.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved