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. Q_ENUMS based model
Forum Updated to NodeBB v4.3 + New Features

Q_ENUMS based model

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 810 Views 3 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.
  • S Offline
    S Offline
    St.Stanislav
    wrote on last edited by
    #1

    Hello all!

    I have faces with the following question: let's suppose that I have Q_ENUMS:

    class ColorType{
    	Q_GADGET
    public:
        enum Value {
            RED_COLOR,
            GREEN_COLOR,
            BLUE_COLOR,
        };
        Q_ENUM(Value);
    
        static void registerQML() {
    	qmlRegisterUncreatableType<ColorType>("My.Enums", 1, 0, "ColorType", "ColorType");
    	qRegisterMetaType<ColorType::Value>("ColorType::Value");
        }
    };
    

    In the QML-side I have combobox, where I want to display this enum, but with the "correct" displaying style ("Red", "Green", "Blue"). So corresponding Q_ENUMS values should be connected with the combobox items. What is the most correct (and simple) way to do this?

    raven-worxR 1 Reply Last reply
    0
    • S St.Stanislav

      Hello all!

      I have faces with the following question: let's suppose that I have Q_ENUMS:

      class ColorType{
      	Q_GADGET
      public:
          enum Value {
              RED_COLOR,
              GREEN_COLOR,
              BLUE_COLOR,
          };
          Q_ENUM(Value);
      
          static void registerQML() {
      	qmlRegisterUncreatableType<ColorType>("My.Enums", 1, 0, "ColorType", "ColorType");
      	qRegisterMetaType<ColorType::Value>("ColorType::Value");
          }
      };
      

      In the QML-side I have combobox, where I want to display this enum, but with the "correct" displaying style ("Red", "Green", "Blue"). So corresponding Q_ENUMS values should be connected with the combobox items. What is the most correct (and simple) way to do this?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @St-Stanislav
      the most generic way (if you do not want to do static mapping) would most probably be to use QMetaEnum inside a QAbstractListModel and publish it to QML.
      Otherwise you simply can use a ListModel and add each list item yourself with it with all custom roles you like.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • S Offline
        S Offline
        St.Stanislav
        wrote on last edited by
        #3

        Thank you for the reply!

        So based on your answer, I have implemented it in the following way:

        1. Have created ColorTypeModel:
        class ColorTypeModel : public QAbstractListModel
        {
            enum {
                ColorTypeIDRole = Qt::UserRole,
                ColorTypeNameRole
        };
        
        1. Have made it singleton:
        public:
            static ColorTypeModel& Instance() {
                static ColorTypeModel colorTypeModel;
                return colorTypeModel;
            }
            virtual ~ColorTypeModel() {}
        private:
            explicit ColorTypeModel(QObject *parent = nullptr);
        
        1. Have added QStringList and static QMap anyway:
        private:
            QStringList m_data;
            static QMap<ColorType::Value, QString> m_namesMap;
        
        1. Then I have implemented fillModel() method and call it in the model's constructor:
        void ColorTypeModel::fillModel()
        {
            m_data.clear();
            const QMetaEnum metaEnum = QMetaEnum::fromType<ColorType::Value>();
            assert((m_namesMap.size() == metaEnum.keyCount()) && "ColorTypeModel map should be filled with names for the every enum item!");
            for (int i = 0; i < metaEnum.keyCount(); ++i) {
                m_data.append(ColorTypeModel::m_namesMap.value(static_cast<ColorType::Value>(metaEnum.value(i))));
            }
        }
        
        1. Have implemented in my backend class (which is set as ContextPropery) method to publish it to QML:
        QVariant Backend::getColorTypeModel() const
        {
            return QVariant::fromValue(&ColorTypeModel::Instance());
        }
        
        1. Have created ComboBox in QML and connect with the aforementioned model:
        ComboBox {
            textRole: "colorTypeName"
            valueRole: "colorTypeID"
            model: backend.getColorTypeModel()
        }
        

        Everything works well! So here is my question: is my approach correct (and match with the described one in your reply)? And can it be simplified somewhere?

        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