PDA

View Full Version : QSortFilterProxy and iterate



mif
6th August 2010, 09:43
Hello,

I have to following problem:

I created my own QAbstractTableModel and QSortFilterProxyModel.

The initialization looks like this:



m_model = new QAlarmModel(ui.tableViewAlarms);
m_proxy = new QAlarmSortFilterProxyModel(ui.tableViewAlarms);
//m_proxy->setDynamicSortFilter(true);
m_proxy->setSourceModel(m_model);
ui.tableViewAlarms->setModel(m_proxy);
ui.tableViewAlarms->sortByColumn(0,Qt::AscendingOrder);


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:



for(int i=0; (i < m_proxy->rowCount()) && (selRow < 0); ++i)
{
int row = i;

QModelIndex idx = m_proxy->index(row,QAlarmModel::COL_ALARM);
QModelIndex idx2 = m_proxy->mapToSource(idx);
QModelIndex idx3 = idx2.sibling(row,QAlarmModel::COL_H_POINTER);
QAlarm *alarm = (QAlarm*)m_model->data(idx3).toLongLong();

if (alarm && !alarm->m_ack)
{
selRow = row;
ui.tableViewAlarms->selectionModel()->reset();
ui.tableViewAlarms->selectionModel()->select(idx, QItemSelectionModel::Select | QItemSelectionModel::Rows);
}
}

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.

caduel
7th August 2010, 09:10
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