סטנלי גרונן
סטנלי גרונן

Reputation: 2997

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

I'm using Qt5 on Windows7.
In my current app, I have a QTableView and I just inserted a new row (at bottom of the table) - as seen below:

enter image description here

After that, I would like to have the cursor ready for editing in the first cell (see above - the red mark) automatically, without the need of a mouse click inside that cell. How can I do it?

Here's the code I have now to insert a new row:

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

Thanks for your time and patience!

Upvotes: 2

Views: 2196

Answers (1)

vahancho
vahancho

Reputation: 21220

You need to identify the cell you want to edit and call QAbstractItemView::edit() function for that model index. For example:

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

Upvotes: 2

Related Questions