PDA

View Full Version : QSortFilterProxyModel updates only first column in view



rain87
2nd February 2011, 12:02
i have met strange issue with QSortFilterProxyModel. i've simplified my code to reflect this issue. suppose, we have a custom item model:
#define COL_CNT 7

class CustomModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit CustomModel(QObject *parent = 0) :
QAbstractItemModel(parent)
{
for(int i = 0; i < COL_CNT; ++i)
cnt << 0;
startTimer(1000);
}
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const
{
if(!parent.isValid())
return createIndex(row, column);
else
return QModelIndex();
}
QModelIndex parent(const QModelIndex &child) const
{
Q_UNUSED(child);
return QModelIndex();
}
int rowCount(const QModelIndex &parent = QModelIndex()) const
{
if(!parent.isValid())
return 1;
else
return 0;
}
int columnCount(const QModelIndex &parent = QModelIndex()) const
{
Q_UNUSED(parent);
return COL_CNT;
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
{
Q_UNUSED(index);
if(role == Qt::DisplayRole)
return QVariant(cnt.at(index.column()));
else
return QVariant();
}

protected:
void timerEvent(QTimerEvent *)
{
for(int i = 0; i < COL_CNT; ++i)
++cnt[i];
emit dataChanged(index(0, 0), index(0, COL_CNT));
}

private:
QList<int> cnt;
};when i connect this model to QTreeView
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CustomModel model;
QTreeView tree;
tree.setModel(&model);
tree.show();
return a.exec();
}i get what i expected - 1 row, 7 columns and increasing counter in every column. then i add generic QSortFilterProxyModel:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CustomModel model;
QSortFilterProxyModel filter;
filter.setSourceModel(&model);
QTreeView tree;
tree.setModel(&filter);
tree.show();
return a.exec();
}and i get following behaviour: in first column (column == 0) counter increases every second, just as planned. but in other columns counter freezes and updates only when i put mouse over, or resize header, or something else

i have resolved this issue in the following way
void TransfersTreeSortFilterModel::mySetSourceModel(QAb stractItemModel *model)
{
setSourceModel(model);
connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this,
SLOT(sourceModelDataChanged(QModelIndex,QModelInde x)));
}

void TransfersTreeSortFilterModel::sourceModelDataChang ed(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
emit dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight));
}but actually i'm not sure this is a good solution

can somebody explain, is this Qt bug or am i missing something?