How to use ListModel.get() method as it always returns QObject type?
-
Hi,
I am using aListModel
along with a delegate which is of theGrid
base type to which I added custom properties and methods.I want to access a particular
Grid
item at indexN
and thought I could use theListModel.get(index)
method.However, as the
get()
method always seems to return typeQObject
, 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 theundefined
error? -
Hi
Cant you just use qobject_cast to cast to your class type to access its properties ? -
You want to access a particular
ListModel
row (defined by aListElement
) or a particular delegate?
What's your usecase and what did you try? Show us some code. -
@mrjj
I need to access it in QML or JavaScript. When I useconsole.debug(myObject.toString())
the returned object type isQObject_XXXXXXXX
but it is actually aGrid
type element. -
Hi
Ok, i though it was the other way :) (on c++ side)
Sorry. I not sure how to cast in JS. -
You want to access a particular
ListModel
row (defined by aListElement
) or a particular delegate?
What's your usecase and what did you try? Show us some code.@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 theGrid
instance at model indexindex
, since I need to use an attached function on thatGrid
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 aListView
, and then it is easy to get theGrid
instance using the standarditemAt(index)
method ofSwipeView
. That app works (for example, the image below shows an example with a single SwipeView page(i.e. aGrid
type element).I only face the problem above because I want to use a
ListView
instead of aSwipeView
as it seems I can achieve better control of the swipe vs. click behavior using aListView
.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 aListView
as in another post of mine https://forum.qt.io/topic/89584/how-to-detect-a-press-on-a-swipeview/7. -
@mrjj
No worries! Thanks, as always, for your kind help.