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. ListView (QML) and QItemSelectionModel (C++)
Forum Updated to NodeBB v4.3 + New Features

ListView (QML) and QItemSelectionModel (C++)

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 2 Posters 2.3k Views
  • 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by fcarney
    #1

    I have a ListView in QML that is accessing a QAbstractListModel from C++. I want to use the QItemSelectionModel with the ListView, but I cannot find the property for the selection model. Does the QML ListView not support that?

    http://doc.qt.io/qt-5/qml-qtquick-listview-members.html

    I want to use it the way it is used in this code:
    http://doc.qt.io/qt-5/qtwidgets-itemviews-chart-mainwindow-cpp.html

    The code calls setSelectionModel on the views that use it. I don't see an equivalent property for ListView in QML.

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      I see that there is a QML type:
      https://doc.qt.io/qt-5.11/qml-qtqml-models-itemselectionmodel.html

      It says it can be used with the model and any views. It also points me to:
      https://doc.qt.io/qt-5.11/qtquick-modelviewsdata-modelview.html

      However I am not seeing how to use it with properly either.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        Well it looks like on the QML side you have to do some work in a mouse click event:
        https://stackoverflow.com/questions/41566115/purpose-and-usage-of-itemselectionmodel-in-qml

        In the delegate for each listview item you need a MouseArea that looks for clicks. Then this will get an index (int) that needs to be converted to a modelindex by calling index on the listmodel. This is then given to the Q/ItemSelectionModel. I am using a C++ one, but the usage is the same on the QML side.

        For some reason I had it in my head that the selection could be inferred through accessing the listmodel only. I guess the listmodel just doesn't have the information to do that.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

          Is QItemSelectionModel supposed to allow views to be synced? I thought this would be a way to make sure all lists that select an item from a list would be synced together. Since there is no direct connection between a ListView and an ItemSelectionModel I don't know how this could work.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            Nitroboy
            wrote on last edited by
            #5

            Have you look at this ? :
            http://doc.qt.io/qt-5/model-view-programming.html#handling-selections-in-item-views

            1 Reply Last reply
            0
            • fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on last edited by
              #6

              @Nitroboy, thank you. I had not read that specific section. That still only covers the widgets side of things in that documentation.

              In the widgets side I can do this:

              table->setSelectionModel(selectionModel);
              

              Where table is a view. This makes table aware of the QItemSelectionModel. I don't see an equivalent for ListView in QML.

              I am thinking I am overthinking this. I can only select one item at a time. I can store this selection whenever the view is clicked. Just storing an index should be sufficient. I had hoped that I could use QItemSelectionModel as a convenient place to store this as it would automagically track this change. I don't see that happening with the ListView in QML.

              C++ is a perfectly valid school of magic.

              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