How to limit the selection in a QTableWidget

╄→гoц情女王★ 提交于 2019-12-05 08:27:09

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!