Results 1 to 10 of 10

Thread: Help with QAbstractProxyModel

  1. #1
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Help with QAbstractProxyModel

    Ok, I am still learning Qt4 and trying to understand how to do things correctly, or as correct as they can be.

    Here is my situation. I want to make use of QDirModel, but it doesn't have everything that I need, so I made an inherited class, MyDirModel.

    MyDirModel adds a few extra columns to QDirModel on population.

    Then I have a couple of QAbstractProxyModels because I want to display the data differently. I have a FolderTreeProxyModel and a FolderListTreeProxyModel. One displays only a few columns and folders only the other displays all columns and files and folders.

    Finally I have two custom QTreeView classes. FolderTree and FolderListTree. Which I set the model FolderTreeProxyModel and FolderListProxyModel respectively.

    Now here's where I am not sure if I am doing this correctly or not. How do I reference indexes from MyDirModel, do I have to reimplement all the sorting searching loading functions?

    Example:
    Qt Code:
    1. ...
    2. connect(folderTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(setRootIndex(QModelIndex)));
    3.  
    4. ...
    5.  
    6. void mainwindow::setRootIndex(QModelIndex index)
    7. {
    8. QModelIndex dir = index.sibling(index.row(), 0);
    9.  
    10. if (!myDirModel->isDir(dir))
    11. {
    12. dir = dir.parent();
    13. }
    14.  
    15. if (dir != folderListTree->rootIndex() && myDirModel->isDir(dir))
    16. {
    17. folderListTree->setCurrentIndex(index);
    18. folderListTree->setCurrentIndex(index);
    19. lineEdit->setText(myDirModel->filePath(dir));
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    This won't work because the index is from MyDirModel? I am kinda confused on this abstraction process any ides/suggestions would be greatly appreciated!

    -KW

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with QAbstractPorxyModel

    There are two things to do:
    1. Always use index objects with models they are owned by.

    2. If an index is from a different model than you expect, convert it to a proper one. I use something like this:
    Qt Code:
    1. while((QAbstractProxyModel *proxy = qobject_cast<QAbstractProxyModel*>(model))!=0){
    2. model = proxy->sourceModel();
    3. index = proxy->mapToSource(index);
    4. }
    To copy to clipboard, switch view to plain text mode 
    After that you'll get an index and model pair referring to the bottom-most (non-proxy) model.

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

    killerwookie99 (12th September 2008)

  4. #3
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help with QAbstractPorxyModel

    I am doing the following and it returns empty path:

    Qt Code:
    1. connect(folderTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(folderTreeClicked(QModelIndex));
    2.  
    3. void mainwindow::folderTreeClicked(QModleIndex &index)
    4. {
    5. QModelIndex dir = folderTreeModel->mapFromSource(index).sibling(index.row(), 0);
    6.  
    7. lineEdit->setText(myDirModel->filePath(dir));
    8.  
    9. cout << lineEdit->text().toStdString() << endl; // always blank
    10. return;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Do I have to re-implement MyDirModel::filePath? Or do change the way I'm getting dir index?

    Thanks,
    -KW

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with QAbstractPorxyModel

    You should be mapping to source, not from source, don't you think? The index you get is the one you clicked in the view and the view's model is the proxy therefore to reach the real dir model you have to convert the index from the proxy to the source model. In doubt you can call QModelIndex::model() to see what model you are dealing with. For instance try:
    Qt Code:
    1. qDebug() << QModelIndex::model()->metaObject()->className();
    To copy to clipboard, switch view to plain text mode 

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

    killerwookie99 (12th September 2008)

  7. #5
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help with QAbstractProxyModel

    I appreciate your help, I am just slow in understanding. So I tried the following in the same function to see what comes out still a blank entry...

    Qt Code:
    1. ...
    2.  
    3. qDebug() << index.model()->metaObject()->className();
    4.  
    5. qDebug() << myDirModel()->fileName(index);
    6.  
    7. ...
    To copy to clipboard, switch view to plain text mode 

    Output:

    FolderTreeModel
    ""

    Thanks for the qDebug() btw, sweet.

    -KW

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with QAbstractProxyModel

    And what about index.row() and index.column()? FolderTreeModel is the proxy isn't it? myDirModel() is of type FolderTreeModel or other?

  9. The following user says thank you to wysota for this useful post:

    killerwookie99 (12th September 2008)

  10. #7
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help with QAbstractProxyModel

    Hmm maybe I am doing this wrong? Here are the class definitions:

    MyDirModel : public QDirModel

    FolderTreeModel : public QAbstractProxyModel
    ->setSourceModel(MyDirModel)

    FolderTreeView : public QTreeView
    ->setModel(FolderTreeModel)

    ... I am trying to display things about files that Qt doesn't understand and I want to be able to control the loading of how QDirModel loads the files, because it doesn't understand the filesystem completely either... Am I doing things in the wrong order?

    -KW

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with QAbstractProxyModel

    Well.. certainly trying to access an index from FolderTreeModel with a MyDirModel is something I would call wrong. When you take an index from the view it references the model set on the view. And in your case this model is the proxy around the base model. To be able to call methods from the base model, you need to convert the index to the proper model. To do that you have to call mapToSource on the proxy, passing it the index taken from the view. In return you will receive an index that is related to the source model of the proxy - in your case the base model you seek to use.

    Qt Code:
    1. QModelIndex index = folderTreeView->currentIndex();
    2. QModelIndex baseIndex = folderTreeModel->mapToSource(index);
    3. QString xyz = myDirModel->fileName(baseIndex);
    To copy to clipboard, switch view to plain text mode 

    Of course I'd do it differently:
    Qt Code:
    1. QModelIndex index = folderTreeView->currentIndex();
    2. const QAbstractProxyModel *proxy = qobject_cast<const QAbstractProxyModel*>(index.model());
    3. while(proxy!=0){
    4. index = proxy->mapToSource(index);
    5. proxy = qobject_cast<const QAbstractProxyModel*>(index.model());
    6. }
    7. QString xyz = myDirModel->fileName(index);
    To copy to clipboard, switch view to plain text mode 

  12. The following user says thank you to wysota for this useful post:

    killerwookie99 (12th September 2008)

  13. #9
    Join Date
    Aug 2008
    Posts
    39
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help with QAbstractProxyModel

    Wow, lol ok, so I see now.

    On your second example, what exactly is this doing:

    Qt Code:
    1. const QAbstractProxyModel *proxy = qobject_cast<const QAbstractProxyModel*>(index.model());
    2.  
    3. while(proxy!=0){
    4. index = proxy->mapToSource(index);
    5. proxy = qobject_cast<const QAbstractProxyModel*>(index.model());
    6. }
    To copy to clipboard, switch view to plain text mode 

    Is it to keep the proxy and index in sync? Thanks for being patient with me

  14. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with QAbstractProxyModel

    I already explained it in post #2. It converts the index to the one belonging to a non-proxy model.

Similar Threads

  1. How to subclass QAbstractProxyModel
    By skycrestway in forum Qt Programming
    Replies: 2
    Last Post: 22nd October 2010, 23:35
  2. QAbstractProxyModel: adding a virtual column
    By ultim8 in forum Qt Programming
    Replies: 6
    Last Post: 28th August 2008, 17:10
  3. QAbstractProxyModel and QTreeView
    By niko in forum Qt Programming
    Replies: 5
    Last Post: 18th January 2008, 21:17
  4. QAbstractProxyModel and "virtual" columns
    By tranfuga25s in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2007, 23:39
  5. Caching QAbstractProxyModel dilemma
    By Alessandro in forum Qt Programming
    Replies: 2
    Last Post: 11th April 2006, 19:51

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.