Select a Single Column in QTableView
-
Hi,
I have a QTableView with a QSelectionModel set on the QTableVoew.
May i know how do i select a particular column only. If i select column number 1 , I should not be allowed to select column number 2. That is at a time more than one column cannot be selected.Thanks
Deepak -
i haven't tried this, but give it a shot:
@
tableView->setSelectionBehavior(QAbstractItemView::SelectColumns);
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
@ -
Thanks.
But tableView->setSelectionBehavior(QAbstractItemView::SelectColumns); selects the entire column i donot want to select the entire column. I just wanted to drag and select only needed elements.
May i know how to do it? -
so you just want to select single cells or do you want to exclude certain cells from a selected row?
for first case just use:
@
tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
@ -
I just wanted to select some values from a single column.
If i selected some values from a column i should not be allowed to select values from another column. That is values from more than one column should not be allowed to select at the same time. -
sorry i don't understand you ... i have no idea what you exactly want.
Maybe you can create some kind of graphic which illustrates what you want? -
Do you want to let the user select an entire column or just (possibly multiple) individual cells in it? If it is individual cells, then you should detect if a user clicks on a cell in a different column. To do that, I would probably try subclassing QTableView and reimplementing selectionChanged() or track mouse clicks by connecting to the clicked() signal. But, as others pointed out, it would help if you provided more information.
-
Exactly i dont want to select entire column. Just multiple individaual cells in a column. If i select some cells from column number 1 and along with that i try to select cells from column number 2 it should not be allowed.
-
is there any pattern in your selection logic? for example that every row can only contain 1 selected col/cell?
i would go for subclassing the model and reimplement "QAbstractItemModel::flags()":http://qt-project.org/doc/qt-4.8/qabstractitemmodel.html#flags
@
Qt::ItemFlags MyModel::flags(const QModelIndex ) const
{
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
//logic to determine if this cell/index should be selectable
if( ! selectable )
flags &= ~Qt::ItemIsSelectable;return flags;
}
@Note: the forum hast problems displaying the "tilde-sign":http://en.wikipedia.org/wiki/Tilde. The minus sign in "flags &= ~Qt::ItemIsSelectable" should be a tilde.
-
It seems to me that you'll need to subclass [[doc:QItemSelectionModel]], and use that custom item selection model on your view. Reimplementing the virtual methods will give you full control over what gets selected and what doesn't.
Alternatively, you can approach it from the model-side of things: just return the right item flags for cells you don't want to be selectable.