我正在尝试在这样的特定单元格上进入编辑模式:
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()
似乎没有工作。
最佳答案
我的天啊!地面吞噬我!
行号从第 0 行开始,我需要做
int r=tm->rowCount(QModelIndex())-1;
QModelIndex id=tm->index(r,ec,QModelIndex());
关于QTableView:如何正确创建 QModelIndex?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2492470/