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. QML/C++ Master/Detail ComboBox/Listview

QML/C++ Master/Detail ComboBox/Listview

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 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.
  • L Offline
    L Offline
    lqsa
    wrote on last edited by
    #1

    On Qt 5.7, Quick Controls 2.0, I have a master ComboBox with a slave ListView. How can the slave change when the user changes the ComboBox selection?

    For exemple:

    Imagine that I have a list of persons, and every person has a list of cars:

    • Person1 - car1, car2, car3
    • Person2 - car4
    • Person3 - car5, car6, car7, car8
    • Person4 - car9, car10
      ...

    The persons must appears on ComboBox and when the users selects a person, the Listview must show person's cars.

    I've tried this, but the carsRole is never called on data member, so the ListView doesn't show anything.

    QML:

    ComboBox {
         textRole: "name"
         model: personsModel
     }
     ListView {
     	model: personsModel.cars
     }
    

    C++

    enum PersonsRoles {
        nameRole = Qt::UserRole + 1,
        carsRole
    };
    	
    QVariant PersonsModel::data(const QModelIndex &index, int role) const
    {
        int row = index.row();
        if ((row < 0) || (row >= _persons.size())) {
            return QVariant();
        }
        switch (role) {
        case nameRole:
            return _persons.at(row);
        case carsRole: {        
            return QVariant::fromValue(new CarsModel(row));
        }
        }
        return QVariant();
    }
    
    QHash<int, QByteArray> PersonsModel::roleNames() const
    {
        QHash<int, QByteArray> roles;
        roles[nameRole] = "name";
        roles[carsRole] = "cars";
        return roles;
    }
    
    M 1 Reply Last reply
    0
    • L lqsa

      On Qt 5.7, Quick Controls 2.0, I have a master ComboBox with a slave ListView. How can the slave change when the user changes the ComboBox selection?

      For exemple:

      Imagine that I have a list of persons, and every person has a list of cars:

      • Person1 - car1, car2, car3
      • Person2 - car4
      • Person3 - car5, car6, car7, car8
      • Person4 - car9, car10
        ...

      The persons must appears on ComboBox and when the users selects a person, the Listview must show person's cars.

      I've tried this, but the carsRole is never called on data member, so the ListView doesn't show anything.

      QML:

      ComboBox {
           textRole: "name"
           model: personsModel
       }
       ListView {
       	model: personsModel.cars
       }
      

      C++

      enum PersonsRoles {
          nameRole = Qt::UserRole + 1,
          carsRole
      };
      	
      QVariant PersonsModel::data(const QModelIndex &index, int role) const
      {
          int row = index.row();
          if ((row < 0) || (row >= _persons.size())) {
              return QVariant();
          }
          switch (role) {
          case nameRole:
              return _persons.at(row);
          case carsRole: {        
              return QVariant::fromValue(new CarsModel(row));
          }
          }
          return QVariant();
      }
      
      QHash<int, QByteArray> PersonsModel::roleNames() const
      {
          QHash<int, QByteArray> roles;
          roles[nameRole] = "name";
          roles[carsRole] = "cars";
          return roles;
      }
      
      M Offline
      M Offline
      medyakovvit
      wrote on last edited by medyakovvit
      #2

      @lqsa One way is to use QStringList for Listview model and add Q_invokable method in model to return cars for current index:

      Q_INVOKABLE QStringList personCars(int index)const{
          // check index is valid
          _persons.at(index)->cars(); 
      }
      

      Then in catch currentIndexChanged in combobox:

      ComboBox {
           textRole: "name"
           model: personsModel
          onCurrentIndexChanged: list.model = personsModel.personCars()
       }
       ListView {
           id: list
       }
      
      1 Reply Last reply
      0
      • L Offline
        L Offline
        lqsa
        wrote on last edited by
        #3

        Thank you,

        Finally I've done this solution: ```
        http://www.qtcentre.org/threads/66408-QML-C-Master-Detail-ComboBox-Listview

        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