开发者

QStringListModel sorting

How can sort QStringListModel?

开发者_开发百科

Thanks a lot.


By using the sort method.


An alternative to the QStringListModel::sort() method is to use the QStringList::sort() method on the string list stored into the model. This approach is not as efficient as using the QStringListModel::sort().

QStringList list = stringListModel->stringList();
list.sort();
stringListModel->setStringList(list);


You can use a QSortFilterProxyModel

QListView* view = new QListView;
QStringListModel* model = new QStringListModel(this);
QSortFilterProxyModel* proxyModel = new QSortFilterProxyModel(this);

proxyModel->setSourceModel(model);
view->setModel(proxyModel);

Then all you have to do is sort your proxy model with the sort method:

void QSortFilterProxyModel::sort(
  int column, Qt::SortOrder order = Qt::AscendingOrder):

Since a string list model has only one column:

proxyModel->sort(0);

Additionally, if you want to sort your model each time a new row is inserted, you can use a connect to sort the proxyModel:

connect(model, &QStringListModel::rowsInserted,
  this, [proxyModel](){ proxyModel->sort(0); });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜