Results 1 to 1 of 1

Thread: mapFromSource and parent

  1. #1
    Join Date
    Oct 2012
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Unix/X11 Windows Android

    Question mapFromSource and parent


    I have three models:
    - main model shown in mainTreeview
    - filter model 1 (with main model as source) shown in filter1Treeview
    - filter model 2 (with main model as source) shown in filter2Treeview

    These models seem to work correctly.

    Now I want to select a row in filter2Treeview based on a selection in filter1Treeview if there is are corresponding counterparts.
    That's what I got so far:
    Qt Code:
    1. foreach(const QModelIndex &row, m_ui->currentTreeView->selectionModel()->selectedRows()) {
    2. QModelIndex counterpartIndex = m_previewGenerator->model()->counterpart(
    3. m_previewGenerator->currentModel()->mapToSource(row), 1);
    4. if(counterpartIndex.isValid()) {
    5. QModelIndex index = m_previewGenerator->previewModel()->mapFromSource(counterpartIndex);
    6. m_ui->previewTreeView->selectionModel()->select(index, QItemSelectionModel::Rows | QItemSelectionModel::ClearAndSelect);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    Using the debugger I found out that counterpartIndex seems to be right. Row, column and parent return the expected values.
    The problem is that mapFromSource doesn't return the index I want. It returns an valid index and row and column have the expected values, too. The problem is the parent. It is always an invalid model index.

    Here's my filter implementation:
    Qt Code:
    1. bool FilteredFileSystemItemModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
    2. {
    3. QModelIndex sourceIndex = sourceModel()->index(sourceRow, 0, sourceParent);
    4. if(sourceIndex.isValid()) {
    5. if(FileSystemItem *item = reinterpret_cast<FileSystemItem *>(sourceIndex.internalPointer())) {
    6. return item->status() == m_statusFilter
    7. || (item->type() == ItemType::Dir && item->children().size());
    8. }
    9. }
    10. return false;
    11. }
    13. bool FilteredFileSystemItemModel::filterAcceptsColumn(int sourceColumn, const QModelIndex &) const
    14. {
    15. switch(m_statusFilter) {
    16. case ItemStatus::Existing:
    17. return sourceColumn == 0 || sourceColumn == 2;
    18. case ItemStatus::New:
    19. return sourceColumn == 1;
    20. default:
    21. return false;
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    I suspect what the problem is (but I don't know if I'm correct and how to solve the problem): The first column isn't present in the second filter model. The column of counterpartIndex is 1 (second column) - that's all right an can be mapped. Its parent however has the column 0. I think the is because the parent implementation in the main model only returns parents with the column 0. I implemented the parent method like this because it seems to be required according to the documentation:
    "A common convention used in models that expose tree data structures is that only items in the first column have children. For that case, when reimplementing this function in a subclass the column of the returned QModelIndex would be 0."

    Has anybody an idea how to solve this problem?

    (The reason why I'm not simply using the QFileSystemModel is that the file system structure I'm dealing with doesn't exist on the system (yet).)

    Edit: I changed the implementation of parent in the main model to create and return indexes with the column of the given index. Now everything works like I expect. But now I'm breaking the rule from the documentation. Because the parent has always the same number of columns as the child so there shouldn't be a problem (and practically there is no a problem either). But I think the way I solved the problem is not very nice nevertheless, so I'm open for other suggestions.
    Last edited by Infinity; 29th March 2014 at 22:16.

Similar Threads

  1. Replies: 3
    Last Post: 22nd March 2014, 16:57
  2. QAbstractProxyModel , mapToSource() and mapFromSource()
    By sajis997 in forum Qt Programming
    Replies: 7
    Last Post: 3rd April 2012, 12:42
  3. Replies: 0
    Last Post: 8th December 2011, 02:00
  4. Help with ::mapFromSource (Proxy Model Question)
    By SSurgnier in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2011, 19:02
  5. Replies: 0
    Last Post: 21st June 2011, 19:27

Tags for this Thread


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.