Results 1 to 3 of 3

Thread: filterAcceptRows() is being called many times for same souceRow.

  1. #1
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default filterAcceptRows() is being called many times for same souceRow.

    Hi,

    I have written my model driven from QSortFilterProxyModel
    and i have implemented the function
    Qt Code:
    1. virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
    To copy to clipboard, switch view to plain text mode 
    but my function is being called repeatedly for same sourceRow, resulting in performance hit.

    Am i missing something?

    Please help me...
    Regards,
    Gk

  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: filterAcceptRows() is being called many times for same souceRow.

    It depends what you do with the model data. If you gave us more info, maybe we could pinpoint the problem. As a solution without knowing your usecase I suggest you cache the result of calculating the return value of filterAcceptsRow so that you don't have to recalculate it all over. This should reduce the impact of what you observe.

  3. #3
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: filterAcceptRows() is being called many times for same souceRow.

    The use case is that i am populating the listView with the filtererd rows.
    Here is the code.

    Qt Code:
    1. class ModelFilterProxyModel : public QSortFilterProxyModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ModelFilterProxyModel(QObject *parent = 0);
    7. void SetSearchStr(QString str);
    8.  
    9. protected:
    10. virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
    11.  
    12. private:
    13. QString m_ManufSearchStr;
    14. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. bool ModelFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
    2. {
    3. bool status = false;
    4. bool found = false;
    5. QModelIndex indexMod = sourceModel()->index(sourceRow, DevicesModel::MODEL, sourceParent);
    6. QString modelStr = sourceModel()->data(indexMod).toString();
    7.  
    8. QModelIndex index = sourceModel()->index(sourceRow, DevicesModel::MANUFACTURER, sourceParent);
    9. QString manufacturetStr = sourceModel()->data(index).toString();
    10. bool found = false;
    11. // Checking for models by the specific manufacturer and eleminating duplicate models.
    12. for (int row = sourceRow; row >= 0 ; --row)
    13. {
    14. QModelIndex index1 = sourceModel()->index(row - 1, DevicesModel::MODEL, sourceParent);
    15. QModelIndex indexManf1 = sourceModel()->index(row - 1, DevicesModel::MANUFACTURER, sourceParent);
    16. QString modelStr1 = sourceModel()->data(index1).toString();
    17. QString manufacturetStr1 = sourceModel()->data(indexManf1).toString();
    18. if (modelStr == modelStr1 && manufacturetStr == manufacturetStr1)
    19. {
    20. found = true;
    21. // No need to search further - so break.
    22. break;
    23. }
    24. }
    25. if (!found && manufacturetStr == m_pPrivate->m_ManufSearchStr)
    26. {
    27. status = true;
    28. }
    29. return status;
    30. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. m_ModelProxyModel->setDynamicSortFilter(true);
    2. m_ModelProxyModel->setSourceModel(m_Model);
    3. m_ModelProxyModel->sort(DevicesModel::MODEL);
    4. m_Ui.modelListView->setModel(m_ModelProxyModel);
    5. m_Ui.modelListView->setModelColumn(DevicesModel::MODEL);
    To copy to clipboard, switch view to plain text mode 

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.