Results 1 to 4 of 4

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

  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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Your QML is trying to access the "cars" property on the "peronsModel" object, not the "cars" role on any specific index.

    For access to the "cars" role you would need a custom delegate, as it has access to the roles of its index through "model.rolename".

    If your PersonsModel instance is only used for that specific combobox, then you could add the property your QML is currently trying to access and have it hold a CarsModel that gets changed whenever the combobox changes its current index.

    Alternatively you could have a function that returns the CarsModel for a given index
    Qt Code:
    1. Q_INVOKABLE CarsModel* carsForRow(int row);
    To copy to clipboard, switch view to plain text mode 
    and call that from QML

    Qt Code:
    1. ListView {
    2. model: personsModel.carsForRow(combo.currentIndex)
    3. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

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

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

    Hi, thank you very much, it works well.

    Only remarks than to avoid to register the CarsModel on QML engine, the Q_INVOKABLE can return a QVariant:
    Qt Code:
    1. Q_INVOKABLE QVariant carsForRow(int row);
    To copy to clipboard, switch view to plain text mode 

    On the method carsForRow only must to convert the CarsModel* to QVariant:
    Qt Code:
    1. return QVariant::fromValue(carsModel);
    To copy to clipboard, switch view to plain text mode 

    Cheers

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    You can also make the method return QObject*

    Cheers,
    _

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.