Results 1 to 6 of 6

Thread: QSelectionModel and QSortFilterProxyModel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QSelectionModel and QSortFilterProxyModel

    Hi,
    I am having test application that uses a tableview with model as CustomProxyModel (inherits from QSortFilterProxyModel) and the customproxymodel has a source model as StandardItemModel(the model has some data).When i tried to read the selected index using the following code:-

    Qt Code:
    1. tableview->selectionModel()->selectedIndexs()
    To copy to clipboard, switch view to plain text mode 

    Everything works fine but when i change my selection behaviour to QAbstractItemView::SelectRows then crash happens.Am i missing some reimplementation methods from sortfilterproxymodel.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSelectionModel and QSortFilterProxyModel

    What happens when the program crashes? What is the stack trace?

    What kind of customization does you proxy model do on top of the QSortFilterProxyModel?

    Cheers,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSelectionModel and QSortFilterProxyModel

    I have attached the stack trace and customization of proxy is depending on the user optionwhich is done by reimplementing methods like :-

    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    QVariant data(const QModelIndex &index, int role) const;
    Qt::ItemFlags flags(const QModelIndex &index) const;
    QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
    QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
    QModelIndex index(int row, int column, const QModelIndex &parent) const;
    QModelIndex parent(const QModelIndex &child) const;
    StackTrace.jpg
    ObjectDetails.jpg

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSelectionModel and QSortFilterProxyModel

    Wow, that is quite a lot of override for a sort filter proxy customization.

    I would have only expected overrides on lessThan() or filterAccepts...()
    Interestingly those are not overwritten.

    Looks like you have a fully custom proxy model, so I would check index+parent for correctness, as well as the map functions.
    Especially parent() can be tricky.

    Given that you are using a table view, are you sure you need custom implementation for methods relevant to a tree model?

    Cheers,
    _

  5. #5
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSelectionModel and QSortFilterProxyModel

    @ando_skoa:
    As this is going to be generic proxy which can be for table or tree view so i need those functions.As you can see below are the implementation for the functions.I am very much confusd why selection model behaves like this when i change the behaviour(SelectRows).

    QModelIndex LiPivotTableProxyModel::mapToSource(const QModelIndex &proxyIndex) const
    {
    return (sourceModel() && proxyIndex.isValid()) ? sourceModel()->index(proxyIndex.row(), proxyIndex.column(), proxyIndex.parent()) : QModelIndex();
    }

    QModelIndex LiPivotTableProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
    {
    return index(sourceIndex.row(), sourceIndex.column(), sourceIndex.parent());
    }
    QModelIndex LiPivotTableProxyModel::index(int row, int column, const QModelIndex &parent) const
    {
    Q_UNUSED(parent);
    return createIndex(row, column, row);
    }

    QModelIndex LiPivotTableProxyModel:arent(const QModelIndex &child) const
    {
    Q_UNUSED(child);
    return QModelIndex();
    }
    As you can see parent i return new QModelIndex for my use case.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSelectionModel and QSortFilterProxyModel

    Since index() and parent() don't do anything yet, you might want to leave their default implementations.

    Your map functions are clearly wrong, since you are calling the respective index() method with a "parent" that doesn't belong to the model.

    The implementations also don't look like they do any structural change, so am wondering why you override them at all.

    Cheers,
    _

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

    anbu01 (18th April 2016)

Similar Threads

  1. QSortFilterProxyModel nothing changes
    By unix7777 in forum Qt Programming
    Replies: 7
    Last Post: 19th August 2012, 08:13
  2. Using QSortFilterProxyModel
    By Jennie Bystrom in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2007, 10:28
  3. QSortFilterProxyModel
    By evgenM in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2007, 11:53

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.