【发布时间】:2016-06-15 06:09:58
【问题描述】:
我有一个带有一些数据的 QTableView。数据有时会发生变化,然后我需要刷新 TableView。刷新后,光标失去位置。如果它在 5 个位置(行),则刷新后超出 tableview(或在第一行)。我希望它恢复到 5,但它不起作用。
我通过“index = ui->tableView->currentIndex()”保存了最后一个位置,并通过“ui->tableView->setCurrentIndex(index)”取回了它
怎么了?
//save last cursor(row) position
QModelIndex index = ui->tableView->currentIndex();
//create basic model with my data
myModel = new MyModel();
//insert my model to sortfilterproxymodel and then sort it
QSortFilterProxyModel *sort_filter = new QSortFilterProxyModel(this);
sort_filter->setSourceModel(myModel);
sort_filter->setSortCaseSensitivity(Qt::CaseInsensitive);
sort_filter->sort(0, Qt::AscendingOrder); //sort by name
sort_filter->sort(5, Qt::DescendingOrder); //sort by surename
//insert my data to tableview
ui->tableView->setModel(sort_filter);
ui->tableView->hideColumn(5);
//return it back to its original position
ui->tableView->setCurrentIndex(index);
【问题讨论】:
标签: c++ qt qtableview