Results 1 to 5 of 5

Thread: DelegateModel: Dynamic group generation, based on another model?

  1. #1
    Join Date
    Apr 2016
    Posts
    9
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default DelegateModel: Dynamic group generation, based on another model?

    Hello!

    I want to create a QML application where I have a bunch of Buttons that you can click on. These Buttons are categorized into groups.

    At the top of the page there are CategoryButtons. When you click on one of the CategoryButtons all the Buttons in that category appear on the page. The Buttons not in that category remain hidden.

    I have two models, one for the CategoryButtons and one for the Buttons.

    Here are the models, just filled with random data:

    Qt Code:
    1. ListModel {
    2. id: categoryButtonsModel
    3. ListElement {
    4. name: "Super Awesome Category"
    5. filterKey: "super"
    6. }
    7.  
    8. ListElement {
    9. name: "Slightly More Awesome Category"
    10. filterKey: "slight"
    11. }
    12. }
    13.  
    14. ListModel {
    15. id: buttonsModel
    16. ListElement {
    17. name: "Cool Button"
    18. filterKey: "super"
    19. }
    20. ListElement {
    21. name: "Geek Button"
    22. filterKey: "super"
    23. }
    24. ListElement {
    25. name: "Hipster Button"
    26. filterKey: "slight"
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    I have added a property to all of these called filterKey. I want all Buttons to be in the category that has the same filterKey that they have. I use a DelegateModel to filter for filterKey:

    Qt Code:
    1. DelegateModel {
    2. id: buttonsDelegateModel
    3. model: buttonsModel
    4. delegate: Button {
    5. text: name
    6. }
    7.  
    8. filterOnGroup: "super"
    9.  
    10. groups: [
    11. DelegateModelGroup {
    12. includeByDefault: false
    13. name: "super"
    14. Component.onCompleted: {
    15. for (var i = 0; i < buttonsModel.count; i++ ) {
    16. var entry = buttonsModel.get(i);
    17. if(entry.filterKey === name) insert(entry);
    18. }
    19. }
    20. },
    21.  
    22. DelegateModelGroup {
    23. includeByDefault: false
    24. name: "slight"
    25. Component.onCompleted: {
    26. for (var i = 0; i < buttonsModel.count; i++ ) {
    27. var entry = buttonsModel.get(i);
    28. if(entry.filterKey === name) insert(entry);
    29. }
    30. }
    31. }
    32. ]
    33. }
    To copy to clipboard, switch view to plain text mode 

    This code above works. I have one problem only:

    The groups property of the above DelegateModel is basically some generic code and the filterKeys in CategoryButtonsModel. I want to generate groups based on CategoryButtonsModel so I don't have to specify the same things two times.

    How can I generate the groups list from CategoryButtonsModel dynamically?

    Thank you for your time.

    Regards,
    Viki

  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: DelegateModel: Dynamic group generation, based on another model?

    You could just show the full list and have each button's visible propert bound to a comparison of their filterKey with the main filter key.
    So all buttons matching that would be visible, all others would not.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2016
    Posts
    9
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: DelegateModel: Dynamic group generation, based on another model?

    Hello!

    Thank you for your answer. I have tried doing what you suggested. The problem is, even if the button is invisible it still occupies space. So the buttons that are visible have empty space between them where the invisible ones are.

    I have tried setting the invisible buttons' width and height to zero. In case of a ListView, this solves the problem.

    In my actual code I have a GridView and it does not work for a GridView. I guess the row and column number does not change for the delegates, even if some cells are invisible. I'm sorry, I thought this wasn't important in my example code.

  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: DelegateModel: Dynamic group generation, based on another model?

    I guess in your actual code you have a real, C++ based, list model anyway, so either add a filter there or use a QSortFilterProxyModel on top of it.

    Cheers,
    _

  5. #5
    Join Date
    Apr 2016
    Posts
    9
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: DelegateModel: Dynamic group generation, based on another model?

    Thank you, I think I will do that.

Similar Threads

  1. Replies: 3
    Last Post: 23rd March 2015, 17:19
  2. Dynamic For generation Problems.
    By ShapeShiftme in forum Qt Programming
    Replies: 4
    Last Post: 21st May 2012, 11:53
  3. Replies: 1
    Last Post: 10th February 2011, 20:32
  4. Replies: 0
    Last Post: 2nd November 2010, 18:54
  5. Lumina GLSL IDE based on QtScript and dynamic Objects
    By oc2k1 in forum Qt-based Software
    Replies: 0
    Last Post: 12th August 2008, 04:12

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.