PDA

View Full Version : QTableWidget select only visible cells



Qiieha
22nd December 2011, 12:14
Hi
A little question:

I have a QTableWidget with lots of rows. I developed a filter, which shows and hides rows.
But if a filter is active, i.e 3 of 300 rows are shown:

001 row
299 row
300 row

If the user selects 001 and 299 all the rows between 1 and 299 are also selected.
How can I solve the problem?

thank u

Spitfire
22nd December 2011, 14:08
Depends on what your filter does and how it does it.

To me it sounds that you're doing something odd that's why you're getting hidden items back (or not doing something).

If you're using QTableWidget then

QModelIndexList QAbstractItemView::selectedIndexes () const [virtual protected]
This convenience function returns a list of all selected and non-hidden item indexes in the view. The list contains no duplicates, and is not sorted.
Should be all you need.

Qiieha
22nd December 2011, 14:24
ok thank you very much..., but I don't want the indexes. I want the rows.
I can't find a method to get the visible rows. So I have to do a workaround with the QModelIndexes?

Added after 6 minutes:

here is my workaround:

I added a method to my QTableWidget:


QList<int> MyTable::getVisibleSelectedRows()
{
QModelIndexList indexes = selectedIndexes();
QList<int> rows;
for(int i = 0; i < indexes.size(); i++){
int row = indexes.value(i).row();
if(!rows.contains(row))
rows.append(row);
}
return rows;
}

Spitfire
22nd December 2011, 15:42
That's redundant.

You're converting list of model indexes without duplicates (selectedIndexes() doesn't contain duplicates) into list of row indexes without duplicates.

And it's what you've said you don't want...

Anyway, I don't think you can get a 'row'.
Having a row index and column index you can get an item, but that's all.

you may use

QList<QTableWidgetItem *> QTableWidget::selectedItems ()
If you want list of selected items, but documentation doesn't say if it takes visibility into account, you'd need to check.

Qiieha
22nd December 2011, 15:52
You are right, there aren't duplicates of QModelIndexes, but there are duplicates of rows.
(One row has i.e. 3 columns, so i get three QModelIndexes and every QModelIndex returns the same row.)
So my method shouldn't be redundant...

Spitfire
23rd December 2011, 09:54
That's correct, I haven't checked deep enough, my bad.

Qiieha
23rd December 2011, 16:38
thank for your help