How to always select an item in a ListView if one is available
-
Hello,
I'm using a QML ListView with a QAbstractListModel/delegate.
I want to make sure that if an item of the ListView is available, then it must be selected, but sometime, my ListView endup with nothing selected, while there is a visible item available.
I'm not sure how to catch the case. So far, I've tried to match "onCurrentItemChanged", "onCurrentIndexChanged", and monitoring view.count, but i can't make it work.
example :
onCurrentIndexChanged : { if (currentIndex == -1) { ListView.view.setSelection(view.count); ListView.view.getSelectedView().setSelected(true); } }
I'm not sure what's wrong here.
Is there an event i can catch when the model change/is reset from the C++ code ? like onModelChanged, but for the content ?
regards,
Marc -
Hello,
I'm using a QML ListView with a QAbstractListModel/delegate.
I want to make sure that if an item of the ListView is available, then it must be selected, but sometime, my ListView endup with nothing selected, while there is a visible item available.
I'm not sure how to catch the case. So far, I've tried to match "onCurrentItemChanged", "onCurrentIndexChanged", and monitoring view.count, but i can't make it work.
example :
onCurrentIndexChanged : { if (currentIndex == -1) { ListView.view.setSelection(view.count); ListView.view.getSelectedView().setSelected(true); } }
I'm not sure what's wrong here.
Is there an event i can catch when the model change/is reset from the C++ code ? like onModelChanged, but for the content ?
regards,
Marc@marcgizmo said in How to always select an item in a ListView if one is available:
Hello,
I'm using a QML ListView with a QAbstractListModel/delegate.
I want to make sure that if an item of the ListView is available, then it must be selected, but sometime, my ListView endup with nothing selected, while there is a visible item available.
Is the goal to always have all items selected, or ensure that at least one is selected in a non-empty view?
I'm not sure how to catch the case. So far, I've tried to match "onCurrentItemChanged", "onCurrentIndexChanged", and monitoring view.count, but i can't make it work.
example :
onCurrentIndexChanged : { if (currentIndex == -1) { ListView.view.setSelection(view.count); ListView.view.getSelectedView().setSelected(true); } }
What ListView is this from? The ListView documentation doesn't mention setSelection(), getSelectedView(), or setSelected().
Is there an event i can catch when the model change/is reset from the C++ code ? like onModelChanged, but for the content ?
All of the signals QAbstractItemModel emits should be valid targets for QML signal handlers. Whether the arguments are usable from QML is another story.
-
@marcgizmo said in How to always select an item in a ListView if one is available:
Hello,
I'm using a QML ListView with a QAbstractListModel/delegate.
I want to make sure that if an item of the ListView is available, then it must be selected, but sometime, my ListView endup with nothing selected, while there is a visible item available.
Is the goal to always have all items selected, or ensure that at least one is selected in a non-empty view?
I'm not sure how to catch the case. So far, I've tried to match "onCurrentItemChanged", "onCurrentIndexChanged", and monitoring view.count, but i can't make it work.
example :
onCurrentIndexChanged : { if (currentIndex == -1) { ListView.view.setSelection(view.count); ListView.view.getSelectedView().setSelected(true); } }
What ListView is this from? The ListView documentation doesn't mention setSelection(), getSelectedView(), or setSelected().
Is there an event i can catch when the model change/is reset from the C++ code ? like onModelChanged, but for the content ?
All of the signals QAbstractItemModel emits should be valid targets for QML signal handlers. Whether the arguments are usable from QML is another story.