Show Last element in QListView

Deadly 提交于 2021-01-29 10:49:29

问题


It sounds trivial, but I could not find the function to show the last added element in a QListView.

It works with a model

// Create model
model = new QStringListModel(this);

// Make data
QStringList List;
// Populate our model
model->setStringList(List);
// Glue model and view together
listView->setModel(model);

Elements are added with

void WidgetMessageList::addString(const QString & message)
{
    if(model->insertRow(model->rowCount())) {
        QModelIndex index = model->index(model->rowCount() - 1, 0);
        model->setData(index, message);        
    }
}

In this function the shown element should also be the last.


回答1:


QAbstractItemView::scrollTo

Scrolls the view if necessary to ensure that the item at index is visible. The view will try to position the item according to the given hint.

http://doc.qt.io/archives/qt-4.8/qabstractitemview.html#scrollTo




回答2:


  1. Create a class attibute to hold the last index
  2. Connect QAbstractItemModel::rowsInserted to a slot in your application
  3. In the slot update the index accordingly


来源:https://stackoverflow.com/questions/52312714/show-last-element-in-qlistview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!