【问题标题】:Show Last element in QListView在 QListView 中显示最后一个元素
【发布时间】:2018-09-13 11:24:54
【问题描述】:

这听起来微不足道,但我找不到在 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);        
    }
}

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

【问题讨论】:

  • QAbstractItemView::scrollTo?
  • 有效! listView->scrollTo(index);
  • 那我们就把它当做答案吧)

标签: c++ qt qlistview qmodelindex


【解决方案1】:

QAbstractItemView::scrollTo

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

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

【讨论】:

    【解决方案2】:
    1. 创建一个类属性来保存最后一个索引
    2. QAbstractItemModel::rowsInserted 连接到应用程序中的插槽
    3. 在槽中相应地更新索引

    【讨论】:

    • 我也会准备一个例子。敬请期待。
    猜你喜欢
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多