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

标签 qt uitableview insert row edit

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

enter image description here

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

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

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);

关于Qt:如何以编程方式开始在 TableView 单元格中进行编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36149870/

相关文章:

mysql - 对于大型插入查询,UNIX_TIMESTAMP() 是否计算一次?

python - 为什么我不能插入 Python 列表?

qt - 如何在不使用 id 的情况下从 ButtonStyle 获取父按钮?

macos - Qt Mac 应用程序无法创建自包含 App Bundle (Qt Creator Build)

ios - 分组的 UITableView 粘性页眉/页 footer 分

ios - 循环颜色列表并将它们设置为单元格背景色 | TableView | objective-C

javascript - 无法从 Meteor 服务器端接收已发布的集合

qt - QLabel 在 QWidget 中不显示

c++ - ITK-VTK vtkResliceImageViewer 未定义

iphone - 动态UITableViewCell高度