Results 1 to 5 of 5

Thread: QTreeView to display only selected subset of items in model

  1. #1
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default QTreeView to display only selected subset of items in model

    Hello,

    how can I connect a QSqlTableModel to a QTreeView in such a way, that only selected items (selected via code) are shown in the QTreeView? Usually I would connect the model to the QTreeView like so:
    Qt Code:
    1. treeView = new QTreeView();
    2. treeView->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    This results in the QTreeView displaying all rows of the underlying SQL table. I would like to be able to only let the QTreeView display items that are set by some function:
    Qt Code:
    1. void MainWindow::addEntry(QModelIndex index)
    2. {
    3. // treeView does know the model, and thus can handle the QModelIndex internally
    4. treeView->addItem(index);
    5. }
    To copy to clipboard, switch view to plain text mode 
    My first intention was to use QTreeWidget instead and manage all items manually. But I would really like to use the same model for another widget. Once the data in the second widget is changed, the QTreeView should also be updated automatically. If I would use a QTreeWidget instead, I would have to tell the other widget's parent (main window) about any changes, and manually propagate them to the QTreeView (and other potential widgets that are not connected to the model). Is there any way to achieve this behavior with the model/view approach?

    Thank you!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTreeView to display only selected subset of items in model

    Use a QSortFilterProxyModel-based class between your SQL table model and your tree view. Reimplement the QSortFilterProxyModel::filterAcceptsColumn() and QSortFilterProxyModel::filterAcceptsRow() methods to include only the items you want.

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

    frankb (3rd January 2014)

  4. #3
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTreeView to display only selected subset of items in model

    Thank you for your answer! Sounds great, but how would I add specific rows in my addEntry() slot? It's supposed to add items on user input. For example, a user double-clicks an entry in a QTableView. I would connect the signal like so:
    Qt Code:
    1. connect(tableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(addEntry(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 
    How would I dynamically adjust the QSortFilterProxyModel filtering (i.e. QSortFilterProxyModel::filterAcceptsRow()) to add this item?

    EDIT: I could manage a list of visible items in the QSortFilterProxyModel to keep track of all visible items, right? Is that a sane way of doing it? Or is there a more convenient way? Thanks again!
    Last edited by frankb; 3rd January 2014 at 17:18.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTreeView to display only selected subset of items in model

    You need to derive your own class from QSortFilterProxyModel and set it up so it keeps track of which items are selected. Either that, or implement a QItemSelectionModel on the view from which the user is selecting items to add to the tree, and set that on the proxy. You're basically setting up a pipeline: your SQL model is the initial source, your tree is the final destination. In between you layer filters and selection models so that what ends up in the tree is whatever the user has selected from the source model via the view they are choosing them from.

    Remember that QModelIndex instances are transient; you can't store them anywhere. So if you store the selected items in the proxy, you'll need to store a list of row numbers, not model indexes.

  6. The following user says thank you to d_stranz for this useful post:

    frankb (4th January 2014)

  7. #5
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTreeView to display only selected subset of items in model

    Works like a charm, thank you! I am using a QList of QPersistentModelIndex to keep track of selected items. From my understanding QPersistentModelIndex works just as well as storing rows though.

Similar Threads

  1. Replies: 4
    Last Post: 31st January 2013, 15:40
  2. Display a subset of the QAbstractItemModel
    By youngw in forum Qt Programming
    Replies: 3
    Last Post: 6th November 2010, 08:33
  3. Replies: 0
    Last Post: 1st November 2010, 08:22
  4. Replies: 0
    Last Post: 1st July 2010, 04:21
  5. Replies: 1
    Last Post: 15th March 2007, 20:45

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.