Results 1 to 8 of 8

Thread: QSortFilterProxyModel + setFilterRegExp for more than 1 column

  1. #1
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default QSortFilterProxyModel + setFilterRegExp for more than 1 column

    Hey @all,

    can anybody told me a way to use one or more setFilterRegExp() calls for more than one column?

    Best Regards
    NoRulez

  2. #2
    Join Date
    Nov 2009
    Location
    PL
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSortFilterProxyModel + setFilterRegExp for more than 1 column

    same problem
    I need to filter content on 2 columns.
    Can I do that without extensive coding ?

  3. #3
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: QSortFilterProxyModel + setFilterRegExp for more than 1 column

    sortfilterproxymodel.h
    Qt Code:
    1. #ifndef SORTFILTERPROXYMODEL_H
    2. #define SORTFILTERPROXYMODEL_H
    3.  
    4. #include <QSortFilterProxyModel>
    5.  
    6. namespace CerSimpleDynamics
    7. {
    8. namespace Models
    9. {
    10. class SortFilterProxyModel : public QSortFilterProxyModel
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit SortFilterProxyModel(QObject *parent = 0);
    15. void setFilterKeyColumns(const QList<qint32> &filterColumns);
    16. void addFilterFixedString(qint32 column, const QString &pattern);
    17.  
    18. protected:
    19. bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
    20.  
    21. private:
    22. QMap<qint32, QString> m_columnPatterns;
    23. };
    24. }
    25. }
    26.  
    27. #endif // SORTFILTERPROXYMODEL_H
    To copy to clipboard, switch view to plain text mode 

    sortfilterproxymodel.cpp
    Qt Code:
    1. #include "sortfilterproxymodel.h"
    2.  
    3. namespace CerSimpleDynamics
    4. {
    5. namespace Models
    6. {
    7. SortFilterProxyModel::SortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
    8. {
    9. }
    10.  
    11. void SortFilterProxyModel::setFilterKeyColumns(const QList<qint32> &filterColumns)
    12. {
    13. m_columnPatterns.clear();
    14.  
    15. foreach(qint32 column, filterColumns)
    16. m_columnPatterns.insert(column, QString());
    17. }
    18.  
    19. void SortFilterProxyModel::addFilterFixedString(qint32 column, const QString &pattern)
    20. {
    21. if(!m_columnPatterns.contains(column))
    22. return;
    23.  
    24. m_columnPatterns[column] = pattern;
    25. }
    26.  
    27. bool SortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
    28. {
    29. if(m_columnPatterns.isEmpty())
    30. return true;
    31.  
    32. bool ret = false;
    33.  
    34. for(QMap<qint32, QString>::const_iterator iter = m_columnPatterns.constBegin();
    35. iter != m_columnPatterns.constEnd();
    36. ++iter)
    37. {
    38. QModelIndex index = sourceModel()->index(sourceRow, iter.key(), sourceParent);
    39. ret = (index.data().toString() == iter.value());
    40.  
    41. if(!ret)
    42. return ret;
    43. }
    44.  
    45. return ret;
    46. }
    47. }
    48. }
    49.  
    50. #include "moc_sortfilterproxymodel.cpp"
    To copy to clipboard, switch view to plain text mode 

    Best Regards
    NoRulez

  4. The following 3 users say thank you to NoRulez for this useful post:

    Exec (4th May 2010), Kyef (23rd September 2016), Thule (7th October 2013)

  5. #4
    Join Date
    Oct 2013
    Location
    Quebec
    Posts
    31
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

  6. #5
    Join Date
    Oct 2013
    Location
    Quebec
    Posts
    31
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSortFilterProxyModel + setFilterRegExp for more than 1 column

    Nevermind, I forgot this important line of code to refresh the proxy
    proxyModel->invalidate();

  7. #6
    Join Date
    May 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel + setFilterRegExp for more than 1 column

    Hello,

    Thank you for that code, but I have a problem When I use this code instead of a QSortFilterProxyModel data from my SourceModel (QStandardItemModel) is not shown.
    Qt Code:
    1. proxyModel->setSourceModel(sourceModel);
    2. sourceModel->setData(source_model->index(0, 1), "test");
    3. qDebug()<<proxyModel->data(proxyModel->index(0,1)).toString();
    To copy to clipboard, switch view to plain text mode 

    With the custom SortFilterProxyModel this returns "" whereas the QSortFilterProxyModel returns "test". The defined column names from the sourceModel are adapted correctly, so the sourceModel is used, only the data is missing.
    Does anyone have an idea how to solve this?

  8. #7
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QSortFilterProxyModel + setFilterRegExp for more than 1 column

    Quote Originally Posted by Tompazi View Post
    With the custom SortFilterProxyModel this returns "" whereas the QSortFilterProxyModel returns "test". The defined column names from the sourceModel are adapted correctly, so the sourceModel is used, only the data is missing.
    Does anyone have an idea how to solve this?
    Hard to tell w/o seeing your custom QSortFilterProxyModel code, but when using the QSortFilterProxyModel, your indexes through the proxy may be different than the indexes from your source model. The whole point of the QSortFilterProxyModel is that you can sort or filter items without having to change your data model items.

    You should have a look at the mapFromSource method to find the proxy model index that corresponds with a source model index. If that doesn't help, please post your code for your custom QSortFilterProxyModel.

    Good luck,

    Jeff

  9. #8
    Join Date
    Oct 2014
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: QSortFilterProxyModel + setFilterRegExp for more than 1 column

    Can you share the code?

Similar Threads

  1. Replies: 2
    Last Post: 2nd December 2008, 05:37
  2. QTreeView: make root decoration appear on visual column 0
    By chezifresh in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2008, 02:36
  3. QSortFilterProxyModel - lessThan sorts wrong column
    By ghorwin in forum Qt Programming
    Replies: 2
    Last Post: 21st July 2008, 09:06
  4. Replies: 0
    Last Post: 10th November 2006, 13:46
  5. hidden QListView column suddenly visible
    By edb in forum Qt Programming
    Replies: 10
    Last Post: 27th January 2006, 08:00

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.