Search code examples
c++qtqtablewidgetselectionmodel

qt QTableWidget programattic select of cell - how to make ready with cursor in?


Environment:

  • Qt 4.7.1
  • Qt Creator 2.1.0
  • c++

Problem:

I have a QTableWidget. I add a row, and then select the first cell.

What I want is to be able to immediately type into the cell after the program selects it, so I don't have to reach for the mouse.

The behavior of the select is to highlight the cell, not put a cursor in it. I have to click on the cell with the mouse before I can type.

Everything I've found so far to do with selection behavior has to do with row, column, or cell selection options; nothing about what the selection of a cell actually does.

Here's my code thus far, works as described; rc is the index to the last row, already determined:

ui->thetable->scrollToBottom();
QModelIndex index = ui->thetable->model()->index(rc, 0);
ui->thetable->selectionModel()->select(index,QItemSelectionModel::Select);

Solution

  • You can use the edit method this way:

    ui->thetable->edit(index);
    

    using the index you already computed, or you could connect a custom signal of yours to void QAbstractItemView::edit ( const QModelIndex & index ) slot inherited by QTableWidget's items.