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 always select an item in a ListView if one is available
Forum Updated to NodeBB v4.3 + New Features

How to always select an item in a ListView if one is available

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.3k 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.
  • M Offline
    M Offline
    marcgizmo
    wrote on last edited by
    #1

    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

    jeremy_kJ 1 Reply Last reply
    0
    • M Offline
      M Offline
      marcgizmo
      wrote on last edited by
      #4

      Using incrementCurrentIndex() to change the selected item, i was able to make it work.

      1 Reply Last reply
      0
      • M marcgizmo

        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

        jeremy_kJ Online
        jeremy_kJ Online
        jeremy_k
        wrote on last edited by
        #2

        @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.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        M 1 Reply Last reply
        0
        • jeremy_kJ jeremy_k

          @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.

          M Offline
          M Offline
          marcgizmo
          wrote on last edited by
          #3

          @jeremy_k

          Having nothing selected when nothing is available is the intended behaviour, it's having nothing selected AND something available that I don't want.

          I'll try to fix my way to set the selection in the ListView.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            marcgizmo
            wrote on last edited by
            #4

            Using incrementCurrentIndex() to change the selected item, i was able to make it work.

            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