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. [Resolved] How to use ENUM?
Forum Update on Monday, May 27th 2025

[Resolved] How to use ENUM?

Scheduled Pinned Locked Moved General and Desktop
13 Posts 6 Posters 21.7k Views
  • 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.
  • D Offline
    D Offline
    dcbasso
    wrote on 29 Oct 2012, 16:35 last edited by
    #1

    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!!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 29 Oct 2012, 16:41 last edited by
      #2

      addItem() takes a QString, so you will have to convert your param by e.g. QString::number(DATA_5) or QLocale().toString(DATA_5).

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dcbasso
        wrote on 29 Oct 2012, 16:42 last edited by
        #3

        I found something about meta-objects:

        http://srikanthsombhatla.wordpress.com/2010/08/13/convert-enum-to-qstring/

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dcbasso
          wrote on 29 Oct 2012, 16:45 last edited by
          #4

          Krzysztof Kawa, Can I make a loop to insert all enum to QComBox?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 29 Oct 2012, 16:56 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dcbasso
              wrote on 29 Oct 2012, 17:02 last edited by
              #6

              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 ONLY

              if 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
              };
              @

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on 29 Oct 2012, 17:03 last edited by
                #7

                So in your case meta-object system is the way to go.

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  issam
                  wrote on 29 Oct 2012, 19:24 last edited by
                  #8

                  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)@

                  http://www.iissam.com/

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on 29 Oct 2012, 20:27 last edited by
                    #9

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

                    }@

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DerManu
                      wrote on 29 Oct 2012, 20:31 last edited by
                      #10

                      Don't restrict the com port baudrate to some unhandy enums. It's just an integer!

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lgeyer
                        wrote on 30 Oct 2012, 05:56 last edited by
                        #11

                        On a sidenote: you do not need to inherit from QObject just for having an meta-object supporting enums. Q_GADGET is sufficient.
                        @
                        struct BaudRate {
                        Q_GADGET
                        Q_ENUMS(BaudRateType)
                        public:
                        enum BaudRateType { ... };
                        };
                        @

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          dcbasso
                          wrote on 31 Oct 2012, 12:50 last edited by
                          #12

                          Well, it's not simple to use. I couldn't use the enum (sorry!), but I select some serial port speeds because the hardware that I communicate has some speed limitations or work better in some speeds!

                          Thanks all.

                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            bzhenel
                            wrote on 16 Apr 2014, 22:17 last edited by
                            #13

                            Thanks a lot!!

                            It's exactly that i'm looking for.

                            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