QTableView中:我如何正确地创建一个QModelIndex?
-
21-09-2019 - |
题
我想在这样一个特定的细胞进入编辑模式:
void MainWindow::on_addButton_released() {
tm->addRow();
tableView->scrollToBottom();
int ec=tm->firstWritableColumn();
int r=tm->rowCount(QModelIndex());
QModelIndex id = tm->index(r, ec, QModelIndex());
tableView->setCurrentIndex(id);
tableView->edit(id);
qDebug() << "row:" << r << " col:" << ec << "index:" << id;
}
我的模型创建索引是这样的:
QModelIndex TableModel::index(int row,int column,QModelIndex parent) const {
Q_UNUSED(parent);
return createIndex(row,column,0);
}
调试输出如下所示:
row: 9 col: 1 index: QModelIndex(9,1,0x0,TableModel(0xbf3f50) )
我相当肯定,该指数是某种无效的,因为setCurrentIndex()
似乎并不奏效。
解决方案
OMG!地吞了我!
行号从0开始行,我需要做
int r=tm->rowCount(QModelIndex())-1;
QModelIndex id=tm->index(r,ec,QModelIndex());
不隶属于 StackOverflow