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. Access selected items in ListView when the button is clicked
Forum Updated to NodeBB v4.3 + New Features

Access selected items in ListView when the button is clicked

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 1.6k Views 2 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.
  • N Offline
    N Offline
    Noturnoz
    wrote on last edited by
    #1

    Hello,

    I have a ListView witch uses my own QAbstractListModel:

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("carListModel", &carListModel);
    

    User can select multiple items and then click a button. When he clicks the button I want to access all the selected items from the c++ code. Can someone tell me how can I achieve that? The part with the button is obvious (signals) but the part where I access the ListView from the c++ is not clear for me.

    The ListView is an arbitrary ListView with assigned model.

    I've read the http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html, but there is a Warning mentioned that the the QObject *rect = object->findChild<QObject*>("rect"); should not be used as it breaks the QML<->c++ logic.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Short Explanation:
      The best would be to set that in the model itself and access it from C++.

      Long Explanation:
      Assuming you have for eg. CarItem in carListModel, you can add a setter and getter function to it. The setter ( say setSelected(bool)) can be made Q_INVOKABLE which will be accessed from QML to set that it is selected. Then just you need to access all these CarItem's from C++ and check if they are selected using getter.

      157

      1 Reply Last reply
      0
      • fecubF Offline
        fecubF Offline
        fecub
        wrote on last edited by
        #3

        I think there is no way to make a QML ListView multi-selectable.
        But you can do something like that:

        if an element is clicked:

        • set its property selected (or however you call it), and
        • set in delegate that if selected is true, then it should be formatted differently. for example the background color changes to yellow.
        • then add it to a list e.g QList of selected items. You can create a property for this in C++
          Later you can get a list of selected items and can handle with this.
        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