3

I work with a QTableView and I would like to develop an advanced selection mode. I have not managed to do it with SelectionBehavior nor with SelectionMode.

The user shall be able to pick up some cells but only if they belong to the same column:

  • VALID BEHAVIOURS:

Select an entire column is OK Select some cells in the same column is OK

  • INVALID BEHAVIOUR:

Select cells in several columns is NOT OK

The icing on the cake would be to prevent the last row (yellow one) to be selected.

Is there any solution?


EDIT - 2013/03/05

I cannot get it work:

ui->table->setSelectionBehavior(QAbstractItemView::SelectItems);
ui->table->setSelectionMode(QAbstractItemView::MultiSelection);

connect(ui->table->selectionModel(), SIGNAL(currentColumnChanged(QModelIndex,QModelIndex)), ui->table->selectionModel(), SLOT(clearSelection()));

What's wrong with this code? I also tried QAbstractItemView::ExtendedSelection and QAbstractItemView::ContiguousSelection.

Maxbester
  • 2,435
  • 7
  • 42
  • 70

1 Answers1

2

Have you checked the signals from QItemSelectionModel, especially

[signal] QItemSelectionModel::currentColumnChanged

could be used to start a new selection in case the current selection would be extended to cover two columns.

Jens
  • 6,173
  • 2
  • 24
  • 43
  • I haven't seen this signal. It could be helpful but when this signal is emitted, how to uncheck the unwanted cell? – Maxbester Feb 28 '13 at 08:24
  • You would probably unselect all cells that have been selected up to now, otherwise, it could be difficult to unselect a selection and start a new selection. So simply start a new selection just with the cell the user selected right now. – Jens Feb 28 '13 at 09:42
  • Yes you're right, thanks. But I've got a problem. `selectionModel()` from my `QTableView` returns null. Therefore, I cannot connect to `currentColumnChanged()`. Does it sound familiar to you? – Maxbester Feb 28 '13 at 10:00
  • You need to set your model first, afterwards, the selectionModel() will return a valid model. If you query selectionModel() before setModel() you will get a null instead of a valid model. – Jens Mar 01 '13 at 07:42