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. Expose QHash to QML

Expose QHash to QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 4.8k Views 2 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
    vlada
    wrote on last edited by
    #1

    I would like to expose a dictionary (QHash<Qstring, int>) to QML UI. If I try it the easy way using Q_PROPERTY, I get following error: QMetaProperty::read: Unable to handle unregistered datatype 'QHash<QString,int>' for property 'Config::options'

    Is there any easy solution to handle this or do I have to implement a QAbstractListModel to expose the data to QML? It seems like an overkill to me.

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi,

      When using custom types like that, you need to register them with the Qt metatype system.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JordanHarris
        wrote on last edited by JordanHarris
        #3

        I'm pretty new to Qml so I'm not entirely sure, but I think you can only make properties of C++/Qt types that have a matching type in Qml. I'm not sure what the best approach would be in your case. Maybe just registering the type in addition to what you're doing would work. Idk. Please report your solution once you find it as I'm curious myself.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vlada
          wrote on last edited by
          #4

          I found 2 good solutions mentioned on Stack Overflow.

          First I tried to implement the solution using QVariantMap. But I had problems with changing the values back from QML. So I tried the second option and used QQmlPropertyMap. This is a very easy solution which doesn't even need to implement any signals when data are changed or writing functions to get changed data from QML.

          This is the solution I was looking for.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved