Results 1 to 3 of 3

Thread: QCompleter for tree model showing only leaves/children

  1. #1
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default QCompleter for tree model showing only leaves/children

    Hello,
    I have a project where I have multiple QComboboxes that I need a QCompleter for. There is a main QCombobox (let's call it parentCombobox) and then there are multiple QComboxes (childComboboxes) whose lists/completions are affected by what is selected in parentCombobox. The data is stored in a tree model using QAbstractItemModl.

    To give a simple food-based example of what I am looking for, let's say that the user selected "Breakfast" in rootCombobox(Meal), then I would like the list in childCombobox1(Protein) to show "eggs", "sausage", "bacon", etc and the list in childCombobox2(Carb) to show "toast", "hash browns", etc. If the user selected "Lunch" from the rootCombobox instead, the lists in the childComboboxes would change to proteins and carbs appropriate to that mealtime. The data would be stored in a model that looks like so:
    Root
    |_Breakfast
    | |_Protein
    | | |_Eggs
    | | |_Sausage
    | | |_Bacon
    | |_Carbs
    | | |_Toast
    | | |_Hash Browns
    |_Lunch
    | |_Protein
    | | |_Sliced Turkey
    | | |_Sliced Ham
    | |_Carbs
    | | |_Bread
    | | |_Chips
    |_Dinner

    And so on and so forth. I have reviewed this example: https://doc.qt.io/qt-5/qtwidgets-too...r-example.html but my problem is that there are no separators because it's not a file system and user would not be typing in a full path to separate. Is there a way I can achieve what I am looking for by subclassing QCompleter as in the example, or do I need to simply get QStringLists of the leaves in the tree and pass them to individual QCompleters for the comboboxes?

    Maybe something that involves using the pathFromIndex function ( https://doc.qt.io/qt-5/qcompleter.html#pathFromIndex ) to create a full path behind the scenes where one doesn't actually exist?
    Last edited by ce_nort; 16th June 2019 at 00:05.

  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: QCompleter for tree model showing only leaves/children

    This sounds like QComboBox::setRootModelIndex() is what you are looking for.

    Essentially you set the tree model on all comboboxes and when the "top level" one changes you set the respective branch model index as the "root" for each of the sub comboboxes.

    I.e. in your example, when the rootCombobox changes to "Breakfast" you retrieve the model index for "Breakfast->Protein" and set that as the root on childCombobox1

    This should result in childCombobox1 only "seeing" the items below that, in your example "Eggs", "Sausage" and "Bacon"

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    ce_nort (17th June 2019)

  4. #3
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: QCompleter for tree model showing only leaves/children

    Ah, that makes sense, thank you!

Similar Threads

  1. Replies: 1
    Last Post: 29th August 2013, 06:41
  2. Replies: 0
    Last Post: 5th September 2011, 09:08
  3. disable a parent and children in a tree model
    By qt_gotcha in forum Newbie
    Replies: 4
    Last Post: 9th July 2009, 07:49
  4. Help on adding children to tree view
    By vieraci in forum Qt Programming
    Replies: 3
    Last Post: 5th June 2009, 08:54
  5. Replies: 1
    Last Post: 12th October 2008, 09:21

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.