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. Why model data isn't exposed as properties of the delegate object?
Forum Updated to NodeBB v4.3 + New Features

Why model data isn't exposed as properties of the delegate object?

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 1.6k 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.
  • F Offline
    F Offline
    Flamaros
    wrote on last edited by
    #1

    For a project menu I have a list view and a sidebar that display properties of selected project. I need to be able to access properties of delegate from the sidebar element.
    For the moment my workaround is to explicitly expose some functions in the delegate to have getters and setters. But I am expecting to be able to get a direct acess to model data from outside the delegate ("projectsListView.currentItem.name") instead of using proxy functions ("projectsListView.currentItem.getName()").

    Sample :
    @
    GridView {
    id: projectsListView

    delegate: Component {
    id: projectsDelegate

    Item {
    id: projectWrapper

    // Workaround because modelData isn't accessible outside the delegate
    function setName(text) {name = text;}
    function getName() {return name;}

    Item {
    width: projectsListView.cellWidth
    height: projectsListView.cellHeight
    x: -parent.x
    y: -parent.y

    ProjectsMenuDelegate {
     id: projectsMenuDelegate
     width: projectsListView.cellWidth
     height: projectsListView.cellHeight
     x: parent.parent.x
     y: parent.parent.y
    

    }
    }
    }

    model: projectsModel;
    }

    TextField {
    text: {return projectsListView.currentItem.getName();}
    }
    @

    Maybe there is a better way to manage that?

    PS : My model is a c++ object that inherit from QAbstractListModel

    1 Reply Last reply
    0
    • U Offline
      U Offline
      unai_i
      wrote on last edited by
      #2

      I cant' see a direct means of achieving this. As a workaround you can try to set an alias property to your modelData to access properties (but I have never tested this so it may not work) and access values as "projectsListView.currentItem.yourAlias.name".

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Flamaros
        wrote on last edited by
        #3

        I already tried this without success, because my model have roles defined (in this case modelData doesn't exist).

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tomma
          wrote on last edited by
          #4

          Delegate is meant for displaying model data in view. Model is the one you should ask for data you want.
          For example:
          @
          TextField {
          text: projectsListView.model.get(projectsListView.currentIndex).name
          }
          @

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Flamaros
            wrote on last edited by
            #5

            In this case I need to create in my c++ model an Q_INVOKABLE method "get" that return an instance of an element, but while the delegate doesn't require to register the element type, it's need for the "get" method that can't return a QVariant like "data" and "setData" methods.

            Doing this, will push me to do the job twice, that is my major issue.

            Roles for element properties are already declared when I create my model derived from QAbstractListModel.

            Notice that solution you propose can't update properties of c++ elements in a better way I actually do (setter in QML).

            [quote author="Tomma" date="1375278023"]Delegate is meant for displaying model data in view. Model is the one you should ask for data you want.
            For example:
            @
            TextField {
            text: projectsListView.model.get(projectsListView.currentIndex).name
            }
            @[/quote]

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Flamaros
              wrote on last edited by
              #6

              Need I request a feature on the bug tracker?

              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