Results 1 to 7 of 7

Thread: Enum To QString Conversion

  1. #1
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Enum To QString Conversion

    Hello,
    I am trying to get text representation of SocketState enum.

    In the header of my class i registered the enum:

    Qt Code:
    1. class QSound;
    2. class SoundNode : public QObject
    3. {
    4. Q_OBJECT
    5. public:
    6. Q_ENUMS(QAbstractSocket::SocketState);
    To copy to clipboard, switch view to plain text mode 

    In the source:

    Qt Code:
    1. QMetaObject metaObject ;
    2. metaObject= QAbstractSocket::staticMetaObject;
    3. QMetaEnum metaEnum = metaObject.enumerator(metaObject.indexOfEnumerator("SocketState" ) );
    4. QString test(metaEnum.valueToKey(QAbstractSocket::ConnectionRefusedError));
    5. qDebug()<<test;
    To copy to clipboard, switch view to plain text mode 

    Here qDebug returns an empty string instead of "ConnectionRefusedError". What is going wrong here, any idea?

    Thanks in advance..

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Enum To QString Conversion

    SocketState is not a registered Q_ENUM in QAbstractSocket therefore its meta-object doesn't know anything about it. Registering it in your class might (I haven't checked) put it into your class's meta-object and not QAbstractSocket's.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Enum To QString Conversion

    Hello Wysota,

    Actually I tried that as seen above:

    Qt Code:
    1. Q_ENUMS(QAbstractSocket::SocketState);
    To copy to clipboard, switch view to plain text mode 

    But not working...

  4. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Enum To QString Conversion

    If enum has meta data then it shouldn't be a problem.
    Your problem is that you definition of enum in one class QAbstractSocket and you try define those meta data in other class SoundNode (use of Q_ENUMS).
    So first try use meta data of you class SoundNode, like this:
    Qt Code:
    1. QMetaEnum metaEnum = SoundNode::staticMetaObject.enumerator(SoundNode::staticMetaObject.indexOfEnumerator("QAbstractSocket::SocketState" ) );
    2. Q_ASSERT(metaEnum.isValid());
    3. QString test(metaEnum.valueToKey(QAbstractSocket::ConnectionRefusedError));
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Enum To QString Conversion

    Read my post again.

    This simple code can be used to list enums registered with a particular class:

    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3.  
    4. int main() {
    5. const QMetaObject & mo = QListView::staticMetaObject;
    6. qDebug() << "Available enums in" << mo.className() << ":" << mo.enumeratorCount();
    7. int i =0;
    8. while((en = mo.enumerator(i++)).isValid()) {
    9. qDebug() << en.name();
    10. }
    11. return 0;
    12. }
    To copy to clipboard, switch view to plain text mode 

    For QListView it returns:

    Available enums in QListView : 13
    Shape
    Shadow
    SelectionMode
    SelectionBehavior
    ScrollHint
    EditTriggers
    ScrollMode
    DragDropMode
    Movement
    Flow
    ResizeMode
    LayoutMode
    ViewMode
    For QAbstractSocket it doesn't return any enums.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Enum To QString Conversion

    This gives, assert error..

    Quote Originally Posted by wysota View Post
    Read my post again.
    Sorry, i think i am missing something. When i declare:
    Qt Code:
    1. Q_ENUMS(QAbstractSocket::SocketState);
    To copy to clipboard, switch view to plain text mode 

    in SoundNode' s header, doesnt it mean that i register that enum for my class?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Enum To QString Conversion

    Yes. For your class. Not for QAbstractSocket. Open moc_soundnote.cpp and see what's in there regarding SocketState (you can see there is nothing there because there is no real enum in your class).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to convert QString to enum?
    By fonzi337 in forum Newbie
    Replies: 4
    Last Post: 21st July 2018, 20:09
  2. Comparing a Qstring with a value defined in an enum
    By AndresBarbaRoja in forum Newbie
    Replies: 5
    Last Post: 24th March 2011, 16:11
  3. QString Unicode conversion
    By user_mail07 in forum Qt Programming
    Replies: 5
    Last Post: 15th April 2010, 22:16
  4. char* to QString: conversion
    By abghosh in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2010, 09:32
  5. qreal -> QString conversion
    By MarkoSan in forum Newbie
    Replies: 2
    Last Post: 13th April 2008, 14:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.