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. How to use ListModel.get() method as it always returns QObject type?
Forum Updated to NodeBB v4.3 + New Features

How to use ListModel.get() method as it always returns QObject type?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 3 Posters 1.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.
  • D Offline
    D Offline
    Diracsbracket
    wrote on 10 Apr 2018, 09:42 last edited by Diracsbracket 4 Oct 2018, 14:28
    #1

    Hi,
    I am using a ListModel along with a delegate which is of the Grid base type to which I added custom properties and methods.

    I want to access a particular Grid item at index N and thought I could use the ListModel.get(index) method.

    However, as the get() method always seems to return type QObject, how can I use that return value? I cannot seem to be able to access any properties or methods on that object, as I always get the undefined error?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 10 Apr 2018, 09:46 last edited by
      #2

      Hi
      Cant you just use qobject_cast to cast to your class type to access its properties ?

      D 1 Reply Last reply 10 Apr 2018, 14:32
      0
      • G Offline
        G Offline
        GrecKo
        Qt Champions 2018
        wrote on 10 Apr 2018, 12:07 last edited by
        #3

        You want to access a particular ListModel row (defined by a ListElement) or a particular delegate?
        What's your usecase and what did you try? Show us some code.

        D 1 Reply Last reply 10 Apr 2018, 14:44
        0
        • M mrjj
          10 Apr 2018, 09:46

          Hi
          Cant you just use qobject_cast to cast to your class type to access its properties ?

          D Offline
          D Offline
          Diracsbracket
          wrote on 10 Apr 2018, 14:32 last edited by
          #4

          @mrjj
          I need to access it in QML or JavaScript. When I use console.debug(myObject.toString()) the returned object type is QObject_XXXXXXXX but it is actually a Grid type element.

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 10 Apr 2018, 14:38 last edited by
            #5

            Hi
            Ok, i though it was the other way :) (on c++ side)
            Sorry. I not sure how to cast in JS.

            D 1 Reply Last reply 10 Apr 2018, 15:47
            1
            • G GrecKo
              10 Apr 2018, 12:07

              You want to access a particular ListModel row (defined by a ListElement) or a particular delegate?
              What's your usecase and what did you try? Show us some code.

              D Offline
              D Offline
              Diracsbracket
              wrote on 10 Apr 2018, 14:44 last edited by Diracsbracket 4 Oct 2018, 15:50
              #6

              @GrecKo
              The code I am currently trying to make it work with is simplified to the following:

              ListView {
                          id: view
                           ...
                          highlightRangeMode: ListView.StrictlyEnforceRange
                          orientation: ListView.Horizontal
                          snapMode: ListView.SnapOneItem
              
                          model: ListModel {
                              id: viewModel
                          }
              
                          function getPageAt(index)
                          {
                              console.debug(viewModel.get(index).toString())
              
                              //I need this returned object to be of type Grid, as used in the delegate
                              return viewModel.get(index)
                          }
              
                          delegate: Grid {
                              visible: true
                              property int dropCount: 0
                              ...
                              columns: gridColumns
                              rows: gridRows
                              ...
              
                              Repeater {
                                  model: grid.columns*grid.rows;
              
                                  delegate: DropTile {
                                      buttonSize: gridButtonSize
                                  }
                              }
              
                              function getDropTargetAt(index)
                              {
                                  return children[index];
                              }
                             ...
                      }
              

              I need my custom attached function getPageAt(index) at to return the Grid instance at model index index, since I need to use an attached function on that Grid instance.

              However, as my first post states, the get() method above returns an object of the type 'QObject_XXXXX'.

              Note: I first implemented the above using a SwipeView instead of a ListView, and then it is easy to get the Grid instance using the standard itemAt(index) method of SwipeView. That app works (for example, the image below shows an example with a single SwipeView page(i.e. a Grid type element).

              0_1523372154100_123b4f49-8216-4032-b1ff-52f24355ea21-image.png

              I only face the problem above because I want to use a ListView instead of a SwipeView as it seems I can achieve better control of the swipe vs. click behavior using a ListView.

              In particular, I want to give the swipe action a higher priority than the press button, so that I can swipe between Grid (-pages) even if the swipe gesture is over a button, without triggering that button.

              Currently, the press has priority it seems. (I am testing on a Windows, however, as it is intended for an Android touch device, I am not sure of the touch/click behavior yet).

              I already did such a replacement of a SwipeView by a ListView as in another post of mine https://forum.qt.io/topic/89584/how-to-detect-a-press-on-a-swipeview/7.

              1 Reply Last reply
              1
              • M mrjj
                10 Apr 2018, 14:38

                Hi
                Ok, i though it was the other way :) (on c++ side)
                Sorry. I not sure how to cast in JS.

                D Offline
                D Offline
                Diracsbracket
                wrote on 10 Apr 2018, 15:47 last edited by Diracsbracket 4 Oct 2018, 15:47
                #7

                @mrjj
                No worries! Thanks, as always, for your kind help.

                1 Reply Last reply
                0

                2/7

                10 Apr 2018, 09:46

                topic:navigator.unread, 5
                • Login

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