Results 1 to 2 of 2

Thread: QSortFilterProxy and iterate

  1. #1
    Join Date
    Jul 2009
    Posts
    3

    Default QSortFilterProxy and iterate

    Hello,

    I have to following problem:

    I created my own QAbstractTableModel and QSortFilterProxyModel.

    The initialization looks like this:

    Qt Code:
    1. m_model = new QAlarmModel(ui.tableViewAlarms);
    2. m_proxy = new QAlarmSortFilterProxyModel(ui.tableViewAlarms);
    3. //m_proxy->setDynamicSortFilter(true);
    4. m_proxy->setSourceModel(m_model);
    5. ui.tableViewAlarms->setModel(m_proxy);
    6. ui.tableViewAlarms->sortByColumn(0,Qt::AscendingOrder);
    To copy to clipboard, switch view to plain text mode 

    Sorting works fine. But I have also a function, which should select the first not acknowledged alarm.
    Therefore i have to iterate through the table and look if the alarm at row x is acknowledged or not.

    I do it like this:

    Qt Code:
    1. for(int i=0; (i < m_proxy->rowCount()) && (selRow < 0); ++i)
    2. {
    3. int row = i;
    4.  
    5. QModelIndex idx = m_proxy->index(row,QAlarmModel::COL_ALARM);
    6. QModelIndex idx2 = m_proxy->mapToSource(idx);
    7. QModelIndex idx3 = idx2.sibling(row,QAlarmModel::COL_H_POINTER);
    8. QAlarm *alarm = (QAlarm*)m_model->data(idx3).toLongLong();
    9.  
    10. if (alarm && !alarm->m_ack)
    11. {
    12. selRow = row;
    13. ui.tableViewAlarms->selectionModel()->reset();
    14. ui.tableViewAlarms->selectionModel()->select(idx, QItemSelectionModel::Select | QItemSelectionModel::Rows);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    The problem is, that the loop doesn't start with the first row of the table. Depending on the sort order it starts with the last.

    I hope someone can help me. Thanks.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxy and iterate

    Well, if you iterate over the proxy, it will be the order in which the proxy presents its data. So, that is not a bug but a, or rather the, feature of the proxy.

    If you want the order of the underlying data, why don't you just iterate over the rows of your m_model instead?

    HTH

Similar Threads

  1. iterate through QHash
    By rosenth in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2010, 07:55
  2. QSortFilterProxy with large table
    By rknowles in forum Qt Programming
    Replies: 12
    Last Post: 23rd September 2009, 13:52
  3. How to iterate QMap twice
    By sophister in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2009, 16:59
  4. Iterate the QListWidget
    By vishal.chauhan in forum Newbie
    Replies: 1
    Last Post: 26th February 2007, 07:51
  5. How to iterate through QButtonGroup's buttons?
    By gadnio in forum Qt Programming
    Replies: 8
    Last Post: 13th January 2006, 05:06

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
  •  
Qt is a trademark of The Qt Company.