1

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);
fyngyrz
  • 2,458
  • 2
  • 36
  • 43
  • Try using [`setEditTriggers`](https://doc.qt.io/qt-5/qabstractitemview.html#editTriggers-prop) to trigger an edit whenever the current item changes. Something like this,`ui->thetable->setEditTriggers(QAbstractItemView::CurrentChanged);` – Mike Oct 15 '16 at 21:39
  • @Mike But this would resolve in having always the cells in edit mode. Even if the user just selects the item it gets in edit mode. I think he wanted it to happen only if programmatically selected. – Luca Angioloni Oct 16 '16 at 00:37
  • @luca-angioloni that's right; now if I could only get the tab order to behave after the programmed select -- the TAB key goes BACKWARDS one to the last of the previous line. So I enter the first data, then press TAB, _then_ I'm in the wrong cell. It works correctly (always TABS to the right / forward) if you mouse in instead of open editing via the program. – fyngyrz Oct 16 '16 at 15:41
  • @fyngyrz that I wouldn't know... I didn't notice that behavior before – Luca Angioloni Oct 16 '16 at 22:48
  • 1
    I ended up having to work around it all by manipulating only `QStringList` elements using external widgets, then setting up the table from those. It appears that several things in `QTableWidget` are broken in qt 4.7, and of course, the qt people have zero interest in making that release meet spec, so there's no point in trying to code to same. I got where I needed to get - the app is 100% functional, so onwards. :) – fyngyrz Oct 17 '16 at 23:50

1 Answers1

2

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.

Luca Angioloni
  • 2,243
  • 2
  • 19
  • 28