5

How would I go about limiting the rows/columns selected in a QTableWidget? I need to force the user to use a contiguous selection (already done) to select exactly two columns and any amount of rows.

Thanks!

Sam Bloomberg
  • 705
  • 1
  • 6
  • 23

1 Answers1

3

You will probably have to do one of 2 things:

  1. You would have to subclass QItemSelectionModel and implement functions for adding and deleting selected QModelIndexes so that you only add items from 2 rows to it.
  2. You can do this by having a custom implementation for catching signals that QItemSelectionModel emits such as:

    connect(tableWidget->selectionModel(), SIGNAL(selectionChanged(QItemSelection &, QItemSelection &)), selectionHandler, SLOT(updateSelection(QItemSelection &, QItemSelection &)));

The selectionHandler is the object that checks the rows and columns of the QModelIndex items in the QItemSelection and remove all Indices that are outside the row range that you would like the user to keep and then:

selectionHandler->ignoreSelectionUpdateSignal();
tableWidget->selectionModel()->select(QItemSelection&);
selectionHandler->acceptSelectionUpdateSignal();

The ignore and accept you need to make sure that you don't get into an infinite loop processing selectionChanged signal.

Karlson
  • 2,958
  • 1
  • 21
  • 48
  • Alright, thanks, I think I'll just go with a simpler way which just gives a message to the user if they select too much, but that does answer my question. – Sam Bloomberg Jan 02 '12 at 15:29