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. ListView model binding to QAbstractListModel derived model
QtWS25 Last Chance

ListView model binding to QAbstractListModel derived model

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 4.8k 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.
  • M Offline
    M Offline
    MichK
    wrote on last edited by
    #1

    I have following QML ButtonMenu element:

    @ Rectangle {
    id: menuRoot
    ...
    property Component model
    ...
    Rectangle {
    id: menu
    ...
    ListView {
    id: menuList
    ...
    model: menuRoot.model;
    ...
    @

    I use it like this:

    @ButtonMenu {
    id: buttonMenu
    ...
    model: menuMainViewArea
    ...
    @

    In C++ code I initiate model:
    @ctxt->setContextProperty("menuMainViewArea",engine.getMenuMainViewArea());
    @

    When I run my program i get following error:
    Error: Cannot assign QObject* to QDeclarativeComponent*

    I tried to make model in menuRoot an alias to menuList model but when I called model.count when setting size of ButtonMenu I got model.count undefined.

    Does anyone know what can be wrong??

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      What does getMenuMainViewArea() return?
      Have you called qmlRegisterType() / qRegisterMetaType() for your model?

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thisisbhaskar
        wrote on last edited by
        #3

        Try this
        @
        Rectangle {
        id: menuRoot
        ...
        property alias model : menuList.model
        ...
        Rectangle {
        id: menu
        ...
        ListView {
        id: menuList
        ...
        //model: menuRoot.model;
        ...
        @

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MichK
          wrote on last edited by
          #4

          Thank you for a quick reply.
          I haven't called qmlRegisterType() / qRegisterMetaType() have to read about them.

          getMenuMainViewArea() retrurns pointer to MenuMainViewArea.

          @
          ...
          MenuMainViewArea* iMainMenu;
          ...
          MenuMainViewArea* Engine::getMenuMainViewArea() const
          {
          return iMainMenu;
          }
          @

          1 Reply Last reply
          0
          • T Offline
            T Offline
            thisisbhaskar
            wrote on last edited by
            #5

            your code as such should work with my proposed changes as long as your MenuMainViewArea is derived class of QAbstractListModel or QStringList

            You don't need to use qmlRegisterType in this case.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MichK
              wrote on last edited by
              #6

              Will check it later. Now I have to do something else :/

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MichK
                wrote on last edited by
                #7

                Using alias works and data from my model is visible in ListView.
                However my problem was a little bit different and it was caused by something else.

                My menu model changes its content depending from application state. In QML I have used
                model.get(i).name and model.count to set appropriate size of my menu. My model from C++ doesn't have count property and get() function. I thought that they are added (like for normal QML model) somehow by framework, but they are not.

                I think that the most reasonable solution is to use ListModel and States in QML.

                Thanks for your help :)

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  thisisbhaskar
                  wrote on last edited by
                  #8

                  or your implement your own count property & get function inside your C++ class which returns model count.

                  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