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. Access model from outside the delegate
Forum Updated to NodeBB v4.3 + New Features

Access model from outside the delegate

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 3 Posters 15.5k 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.
  • Q Offline
    Q Offline
    qtnext
    wrote on last edited by
    #1

    Hi,

    I have a listview, a Delegate and a c++ AbstractItemModel. I have a side panel at the right of the listview. When I click on a item I wants to set a binding between current model index selected and the right panel.
    The dirty methods is to embed the right panel as child in the delegate of the listview but it's seems useless that each item draw has a right side panel. So I need to be able to access the model and the current Index outside of the listview. I have tryed using "http://lists.qt.nokia.com/pipermail/qt-qml/2010-December/001830.html" but it don't works at all.

    1 Reply Last reply
    1
    • M Offline
      M Offline
      mohsen
      wrote on last edited by
      #2

      would you like to put your QML code here for better assistant?

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xsacha
        wrote on last edited by
        #3

        My understanding:
        You just want to change the ListView's currentItem when you select an item from the right of the Listview.

        You have something like:
        @ListView {
        id: myList
        model: myAbstractModelFromC
        }@

        There are a few ways to do this:
        @
        myList.currentItem = panel.currentItem
        @
        OR
        @
        MyPanel {
        property alias currItem: myList.currentItem
        currentItem: currItem
        }
        @
        OR
        @
        MyPanel {
        currentItem: myList.currentItem
        }
        @

        You can have the 'currentItem' variable in a global property if need be.
        Am I misunderstanding something?

        • Sacha
        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qtnext
          wrote on last edited by
          #4

          Hi,

          here an example : (sorry ... it's more easier to understand :)
          @import QtQuick 1.0

          Rectangle {
          width: 600; height: 480
          Component {
          id:deleg
          Row {
          Rectangle {
          width: 100; height: 30
          Text {
          anchors.fill: parent
          text: model.label
          }
          MouseArea {
          anchors.fill: parent
          onClicked: {
          listview.currentIndex=index;
          }
          }
          }
          }
          }

          ListView {
              id: listview
              model: externalmodel
              delegate: deleg   
          }
          
          Rectangle {
              id: extrapanel
              anchors.left: listview.right
              Text {
                  id: extrapaneltext
                  text: listview.model.currentItem.label
              }
          }
          

          }@

          In the delegate I can access without trouble my model, but I wants to have a binding between data in model of the currentItem in extrapanel (Imagine extrapanel is an heavy component with a lot of things). For now the only solution I have find is to add extrapanel in the delegate, change visibility to true of the currentIndex (and false for other) and reparent the extrapanel to the mainpanel to set the position ....

          I Imagine that there is another way and it seems it's a quite common situation : you have a list and a model, when you select an item you wants to bind the current index to the panel of the right.

          I hopes it's more clear ...

          1 Reply Last reply
          0
          • X Offline
            X Offline
            xsacha
            wrote on last edited by
            #5

            Still not clear for me :
            Why can't you access it form extrapanel?

            By the way, what's with your text property? The model doesn't have any 'current' properties.
            It should be something like:
            @text: listview.model.get(listview.currentIndex).label@
            Is that what you had trouble with?

            • Sacha
            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              qtnext
              wrote on last edited by
              #6

              I wants to have a binding with the current index of the model like in the delegate but outside of the delegate. (so if property of the current index change in the model, the data will be automatically updated in the extrapanel).

              1 Reply Last reply
              0
              • X Offline
                X Offline
                xsacha
                wrote on last edited by
                #7

                Either in the extrapanel:
                @Binding { target: extrapaneltext; property: "text"; value: listview.model.get(listview.currentIndex).label }@

                Or in the item:
                @Binding { target: extrapaneltext; property: "text"; value: label; when: list.ListView.isCurrentItem }@

                See bottom of this page: http://doc.qt.nokia.com/4.7-snapshot/qml-binding.html

                • Sacha
                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  qtnext
                  wrote on last edited by
                  #8

                  I have tryed and I have the following error : TypeError: Result of expression 'outputListView.model.get' [undefined] is not a function.

                  1 Reply Last reply
                  0
                  • X Offline
                    X Offline
                    xsacha
                    wrote on last edited by
                    #9

                    The documentation definitely has a 'get' function for ListModel (http://doc.qt.nokia.com/4.7-snapshot/qml-listmodel.html).
                    Maybe the QAbstractListModel doesn't expose the same function then.
                    You may need to code it in (C++ side).

                    My second example (within item) should still work for you without needing a get function.

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