Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML ComboBox and a C++ enum list

QML ComboBox and a C++ enum list

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 4 Posters 3.4k 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.
  • W Offline
    W Offline
    wincak
    wrote on 3 Oct 2019, 08:29 last edited by
    #1

    Hi guys,

    I keep bumping into a problem of using QML ComboBox to choose an option from a list of enum values provided by C++ backend. Since I'm sort of new to QML this might be a stupid question, however I haven't found an acceptable answer anywhere yet.

    Suppose there is a C++ class that exposes a property

    Q_PROPERTY(Color color READ color WRITE setColor NOTIFY colorChanged)
    

    Color is an enum defined by the class

    enum class Color {
        Red,
        Green,
        Blue,
    };
    

    And the class provides a list of currently available options to choose from - the list can contain all the defined enum values or even none. There is a corresponding "changed" signal as well.

    QList<Color> availableColors() const;
    signals:
    void availableColorsChanged(QList<Color>);
    

    All this is basic stuff on C++ side. But now I need a QML ComboBox in the user interface that would list translatable names of all currently available colors and that would set the "color" property based on user input.

    I have tried several approaches so far (wrapping Color enum in a class to provide the list as QList<QObject*>, using models with named roles, using strings instead of enum values, ...) but all of them require a lot of boilerplate code and/or feel hackish.

    What is the "right" way to handle this?

    1 Reply Last reply
    1
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 3 Oct 2019, 09:42 last edited by
      #2

      If you want translatable color name, you need to implement your own logic. It can be either in C++ or QML. You need to write the boiler plate code.

      Instead of exposing the color enums, can you expose color names itself ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      1
      • G Offline
        G Offline
        GrecKo
        Qt Champions 2018
        wrote on 3 Oct 2019, 09:46 last edited by
        #3

        You could get the string representations of your enums by using QMetaObject and QMetaEnum.
        There's no such thing existing out of the box though, you would have to implement a C++ helper accessible in QML.

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wincak
          wrote on 3 Oct 2019, 14:06 last edited by
          #4

          @dheerendra said in QML ComboBox and a C++ enum list:

          If you want translatable color name, you need to implement your own logic. It can be either in C++ or QML. You need to write the boiler plate code.

          Of course, translation logic has to exist. My idea would be to put it somewhere in the QML frontend as close to the combo itself as possible so the values are expressed as enums everywhere and the program doesn't have to shuffle strings around (making it more efficient and easier to maintain).
          Something like using a following model for the combo:

          ListModel {
              ListElement { text: qsTr("Red") value: Color.Red }
              ListElement { text: qsTr("Green") value: Color.Green }
              ListElement { text: qsTr("Blue") value: Color.Blue }
          }
          

          However I haven't found an easy way to remove unavailable options from a model like this.

          @dheerendra said in QML ComboBox and a C++ enum list:

          Instead of exposing the color enums, can you expose color names itself ?

          Exposing names themselves would mean using hard-to-maintain strings instead of simple enum values. The code is more clear if you use Color.Red instead of "red". Also if classes' color parameter is used by other C++ code as well, it would be necessary to duplicate setter and getter functions (one set with enums and second with strings).

          @GrecKo said in QML ComboBox and a C++ enum list:

          You could get the string representations of your enums by using QMetaObject and QMetaEnum.
          There's no such thing existing out of the box though, you would have to implement a C++ helper accessible in QML.

          That is possible, however you get untranslated strings only. So you basically have to convert
          enum <---> untranslated string <---> translated string
          which feels a bit clumsy.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adaitabehera
            wrote on 9 Mar 2021, 09:09 last edited by adaitabehera 3 Sept 2021, 09:10
            #5
            enum class Color {
                Red,
                Green,
                Blue
            };
            Q_ENUM(Color)
            
            QMetaEnum metaEnum = QMetaEnum::fromType<Color>();
            QStringList enumStrings;
            for(int i = 0; i < metaEnum.keyCount(); ++i)
            {
                enumStrings.append(metaEnum.key(i));
            }
            

            C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg. -- Bjarne Stroustrup

            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