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 : How to read a QList from C++
Forum Updated to NodeBB v4.3 + New Features

QML : How to read a QList from C++

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 3.0k 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.
  • C Offline
    C Offline
    Craig27
    wrote on last edited by
    #1

    I'm trying to pass QList of integer from QML to C++ code, but somehow my approach is not working.

    Any inputs to solve the problem is highly appreciated.

    Below is my code snippet

    @_Header file_

    Q_PROPERTY(QDeclarativeListProperty<int> enableKey READ enableKey) 
    
    QDeclarativeListProperty<int> enableKey(); //function declaration
    QList<int> m_enableKeys;
    

    cpp file

    QDeclarativeListProperty<int> KeyboardContainer::enableKey()
    {
    return QDeclarativeListProperty<int>(this, 0, &KeyboardContainer::append_list);
    }

    void KeyboardContainer::append_list(QDeclarativeListProperty<int> *list, int *key)
    {
    int *ptrKey = qobject_cast<int *>(list->object);
    if (ptrKey) {
    key->setParentItem(ptrKey);
    ptrKey->m_enableKeys.append(key);
    }
    }@

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chrisadams
      wrote on last edited by
      #2

      QDeclarativeListProperty is not what you think it is. Don't use it.

      If you were using QtQuick2, you could simply use QList<int> but in QtQuick1 you're basically out of luck. One terrible hack is to use a QVariantMap and insert(index, value). But it will be exposed in JS as an object, not an array.

      Cheers,
      Chris.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Craig27
        wrote on last edited by
        #3

        Thanks for the reply :)

        1 Reply Last reply
        0
        • 4 Offline
          4 Offline
          416365416c
          wrote on last edited by
          #4

          For your specific case a better work around could be to just expose an append(int) function to QML directly and iterate over the JS array contents in QML.

          [quote author="chrisadams" date="1355987161"]QDeclarativeListProperty is not what you think it is. Don't use it.
          [/quote]

          To be a little more helpful, QDeclarativeListProperty is only for lists of QObject* or pointers to QObject derived types. Not for primitive types.

          Also QList<int> works fine for exporting lists from C++ to QML, better than QVariantMap. But it won't work as you'd expect for assigning to it in QML.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Craig27
            wrote on last edited by
            #5

            The discussion really gave me better insight to the concepts :)

            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