在QListView中显示最后一个元素

问题描述 投票:0回答:2

这听起来微不足道,但我找不到函数来显示QListView中最后添加的元素。

它适用于模型

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

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

添加元素

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

在此函数中,显示的元素也应该是最后一个。

c++ qt qlistview qmodelindex
2个回答
1
投票

QAbstractItemView中:: scrollTo

如有必要,滚动视图以确保索引处的项目可见。视图将尝试根据给定的提示定位项目。

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


0
投票
  1. 创建一个类属性来保存最后一个索引
  2. QAbstractItemModel::rowsInserted连接到应用程序中的插槽
  3. 在插槽中相应地更新索引
© www.soinside.com 2019 - 2024. All rights reserved.