Qt:如何以编程方式在 TableView 单元格中开始编辑?

Qt: How to programmatically start editing in a TableView cell?

我在 Windows7.
上使用 Qt5 在我当前的应用程序中,我有一个 QTableView 并且我刚刚插入了一个新行(在 table 的底部)- 如下所示:

之后,我想让光标自动准备好在第一个单元格(见上图 - 红色标记)中进行编辑,而无需在该单元格内单击鼠标。我该怎么做?

这是我现在插入新行的代码:

void MyTable::addNewRow()
{
    model->insertRow(model->rowCount());
    ui->tableView->scrollToBottom();
    // ??? to programmatically start editing in 1st cell
    // ...
}

感谢您的耐心等待!

您需要确定要编辑的单元格并为该模型索引调用 QAbstractItemView::edit() 函数。例如:

int rows = ui->tableView->model()->rowCount();

// Get the last row's model index (first column)
QModelIndex index = ui->tableView->model()->index(rows - 1, 0);

// Start editing the cell
ui->tableView->setCurrentIndex(index);
ui->tableView->edit(index);