Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Pass QMap type to QML
Qt 6.12 LTS is out! See what's new in the release blog

Pass QMap type to QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 4 Posters 2.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.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on last edited by Vinoth Rajendran4
    #1

    Hi All,
    Is it possible to pass QMap<T1,T2> type as types in Q_PROPERTY and be possible to access them in QML ?

    I could see we can use QVariantMap, but i dont want to pass QString as key. I need to pass an enumerator as key .

    Can someone point me in correct direction.

    Thanks in advance.

    raven-worxR K 2 Replies Last reply
    0
    • V Vinoth Rajendran4

      Hi All,
      Is it possible to pass QMap<T1,T2> type as types in Q_PROPERTY and be possible to access them in QML ?

      I could see we can use QVariantMap, but i dont want to pass QString as key. I need to pass an enumerator as key .

      Can someone point me in correct direction.

      Thanks in advance.

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

      @Vinoth-Rajendran4
      using an enumerator as key is not JSON conform, no way around it.
      So there is no direct way to support any types other the ones mentioned here: https://doc.qt.io/qt-5/qtqml-cppintegration-data.html
      For everything else you need to create custom QML types or QObject based wrappers for example.

      --- 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
      1
      • kenneth.fernandezK Offline
        kenneth.fernandezK Offline
        kenneth.fernandez
        wrote on last edited by
        #3

        Hi @Vinoth-Rajendran4

        @raven-worx is right. Here is an example of a custom C++ type that maybe could help you:

        class QCustomMap: public QObject
        {
           Q_OBJECT
        public :
           QCustomMap();
        
           Q_INVOKABLE int getId(int id) const
           {
                return _someMap.contains(id) ? _someMap[id] : 0;
           }
        
        private:
            const static QMap<int, int> _someMap;  ///< Map for your elements
        }
        

        But It depends on what you want to do and your base code, maybe what you want is to use only a Q_INVOKABLE in your current QObject instead a Q_PROPERTY to get/update the values and also create/register a Q_ENUMS for you to be able to use the ENUM on QML, let me know what is your plan or architecture and I will send you some additional code as reference. I hope this would help you for the moment.

        --
        Lic-Ing. Kenneth Fernández Esquivel
        Embedded Software Engineer
        RidgeRun Engineering Ltd.
        www.ridgerun.com
        Email: kenneth.fernandez@ridgerun.com
         San Juan, La Unión. Cartago. Costa Rica

        1 Reply Last reply
        0
        • V Vinoth Rajendran4

          Hi All,
          Is it possible to pass QMap<T1,T2> type as types in Q_PROPERTY and be possible to access them in QML ?

          I could see we can use QVariantMap, but i dont want to pass QString as key. I need to pass an enumerator as key .

          Can someone point me in correct direction.

          Thanks in advance.

          K Offline
          K Offline
          Kofr
          wrote on last edited by
          #4

          @Vinoth-Rajendran4 QML works well with QVariantMap

          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
          • Unsolved