No it is not that complicated, but the name of the class states it is an abstract type as in QAbstractItemModel.
I guess that is the reason why most of the methods have empty implementations or left as pure abstracts.
Alternatively tried to subclass from QStandardItemModel or another more appropriate model if such exists.
Please take a look at the following url
http://cep.xor.aps.anl.gov/software/...c86413b4fa648e
{
QVector<QPair<QTableWidgetItem*, int> > sortable;
QVector<int> unsortable;
sortable.reserve(rowCount());
unsortable.reserve(rowCount());
for (int row = 0; row < rowCount(); ++row) {
sortable.append(QPair<QTableWidgetItem*,int>(itm, row));
else
unsortable.append(row);
}
LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
qStableSort(sortable.begin(), sortable.end(), compare);
emit layoutAboutToBeChanged();
QVector<QTableWidgetItem*> sorted_table(tableItems.count());
QModelIndexList from;
QModelIndexList to;
for (int i = 0; i < rowCount(); ++i) {
int r = (i < sortable.count()
? sortable.at(i).second
: unsortable.at(i - sortable.count()));
for (int c = 0; c < columnCount(); ++c) {
sorted_table[tableIndex(i, c)] = itm;
from << createIndex(r, c, 0);
to << createIndex(i, c, 0);
}
}
tableItems = sorted_table;
changePersistentIndexList(from, to); // ### slow
emit layoutChanged();
}
{
QVector<QPair<QTableWidgetItem*, int> > sortable;
QVector<int> unsortable;
sortable.reserve(rowCount());
unsortable.reserve(rowCount());
for (int row = 0; row < rowCount(); ++row) {
if (QTableWidgetItem *itm = item(row, column))
sortable.append(QPair<QTableWidgetItem*,int>(itm, row));
else
unsortable.append(row);
}
LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
qStableSort(sortable.begin(), sortable.end(), compare);
emit layoutAboutToBeChanged();
QVector<QTableWidgetItem*> sorted_table(tableItems.count());
QModelIndexList from;
QModelIndexList to;
for (int i = 0; i < rowCount(); ++i) {
int r = (i < sortable.count()
? sortable.at(i).second
: unsortable.at(i - sortable.count()));
for (int c = 0; c < columnCount(); ++c) {
QTableWidgetItem *itm = item(r, c);
sorted_table[tableIndex(i, c)] = itm;
from << createIndex(r, c, 0);
to << createIndex(i, c, 0);
}
}
tableItems = sorted_table;
changePersistentIndexList(from, to); // ### slow
emit layoutChanged();
}
To copy to clipboard, switch view to plain text mode
Please notice the text snippet below taken from the API description.
** void QAbstractItemModel::layoutAboutToBeChanged() [signal]
Since: 4.2
This signal is emitted just before the layout of a model is changed.
Components connected to this signal use it to adapt to changes in the model's layout.
Subclasses should update any persistent model indexes after emitting layoutAboutToBeChanged().
** void QAbstractItemModel::layoutChanged() [signal]
This signal is emitted whenever the layout of items exposed by the model has changed;
for example, when the model has been sorted. When this signal is received by a view,
it should update the layout of items to reflect this change. When subclassing
QAbstractItemModel or QAbstractProxyModel, ensure that you emit layoutAboutToBeChanged()
before changing the order of items or altering the structure of the data you expose to
views, and emit layoutChanged() after changing the layout.
Subclasses should update any persistent model indexes before emitting layoutChanged().
** void QAbstractItemModel::layoutAboutToBeChanged() [signal]
Since: 4.2
This signal is emitted just before the layout of a model is changed.
Components connected to this signal use it to adapt to changes in the model's layout.
Subclasses should update any persistent model indexes after emitting layoutAboutToBeChanged().
** void QAbstractItemModel::layoutChanged() [signal]
This signal is emitted whenever the layout of items exposed by the model has changed;
for example, when the model has been sorted. When this signal is received by a view,
it should update the layout of items to reflect this change. When subclassing
QAbstractItemModel or QAbstractProxyModel, ensure that you emit layoutAboutToBeChanged()
before changing the order of items or altering the structure of the data you expose to
views, and emit layoutChanged() after changing the layout.
Subclasses should update any persistent model indexes before emitting layoutChanged().
To copy to clipboard, switch view to plain text mode
Hth
Bookmarks