Results 1 to 4 of 4

Thread: QML/C++ Master/Detail ComboBox/Listview

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2014
    Posts
    33
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows Android

    Default QML/C++ Master/Detail ComboBox/Listview

    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:

    Qt Code:
    1. ComboBox {
    2. textRole: "name"
    3. model: personsModel
    4. }
    5. ListView {
    6. model: personsModel.cars
    7. }
    To copy to clipboard, switch view to plain text mode 

    C++

    Qt Code:
    1. enum PersonsRoles {
    2. nameRole = Qt::UserRole + 1,
    3. carsRole
    4. };
    5.  
    6. QVariant PersonsModel::data(const QModelIndex &index, int role) const
    7. {
    8. int row = index.row();
    9. if ((row < 0) || (row >= _persons.size())) {
    10. return QVariant();
    11. }
    12. switch (role) {
    13. case nameRole:
    14. return _persons.at(row);
    15. case carsRole: {
    16. return QVariant::fromValue(new CarsModel(row));
    17. }
    18. }
    19. return QVariant();
    20. }
    21.  
    22. QHash<int, QByteArray> PersonsModel::roleNames() const
    23. {
    24. QHash<int, QByteArray> roles;
    25. roles[nameRole] = "name";
    26. roles[carsRole] = "cars";
    27. return roles;
    28. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by lqsa; 19th July 2016 at 03:58.

Similar Threads

  1. Replies: 2
    Last Post: 16th May 2016, 23:50
  2. Clone QGraphicsView (master-slave display)
    By quimnuss in forum Qt Programming
    Replies: 19
    Last Post: 25th September 2015, 16:50
  3. couldnt create slave
    By prophet0 in forum Qt Programming
    Replies: 0
    Last Post: 19th December 2011, 17:50
  4. How to set custom height for ListView & ComboBox items
    By rawfool in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2011, 10:39
  5. Replies: 5
    Last Post: 15th April 2011, 11:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.