PDA

View Full Version : How to find if a specific row is included in a selection?



IndikaU
13th September 2011, 18:15
Hi,

I'm new to Qt programming and struggling with QTableView to do a custom selection. What I need to achieve is enabling some buttons depending on how rows are selected.

I've set selectionBehavior to SelectItems & selectionMode to ContiguousSelection. This is to easily find if a row is selected.

I'm using the selectionChanged SIGNAL to get the current selection and use following code fragment to get the list of selected rows.

//Initial setup - 3 columns, 3 empty rows

p_TableFields = new QStandardItemModel(this);

p_TableFields->insertColumns(0, 3);
p_TableFields->insertRows(0, 3);

p_TableFields->setHeaderData(0, Qt::Horizontal, tr("Field Name"));
p_TableFields->setHeaderData(1, Qt::Horizontal, tr("Data Type"));
p_TableFields->setHeaderData(2, Qt::Horizontal, tr("Description"));

tblFields->setModel(p_TableFields);

connect(tblFields->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(OnSelect(const QItemSelection&, const QItemSelection&)));

//OnSelect
QModelIndexList RowsSelected = tblFields->selectionModel()->selectedRows();

Now the question is how can find if the first row is included in the selection? This is basically to decide on enabling a Move Rows Up button only if first row is not selected.

Similar logic need to implement for Move Rows Down button if last row is not selected.

6847

llev
13th September 2011, 21:59
QModelIndexList RowsSelected = tblFields->selectionModel()->selectedRows();

Now the question is how can find if the first row is included in the selection? This is basically to decide on enabling a Move Rows Up button only if first row is not selected.


Do you mean the first row of the table?
If so you could just iterate RowsSelected to determine whether the index with row()==0 is in the list.
Doesn't it work for you?

ChrisW67
13th September 2011, 22:47
The selectionChanged() signal gives you the new QItemSelection. If you have set the view to select rows then you can see if the first/last row is in the selection with by using QItemSelection::contains() with the QModelIndex of the first/last row and a visible column. If you allow selecting arbitrary items then you would have to check each item in the first/last row or you could also grab the selectionModel() of the view and use: QItemSelectionModel::isRowSelected().

You need to be careful to handle the case when both the first and last row are in the selection or when the view is empty.

IndikaU
14th September 2011, 02:36
Hi llev,

Actually I'm looking for a method to check if first/last row in the table is included within a selection. Above code fragment is used for get the list of rows which are entirely selected.

Is there any function to check whether first row is selected without iterating the list?

Added after 16 minutes:

Hi Chris,

I'm using the selection as you mentioned. I never allow arbitrary selections. As I'm new to Qt I need to know how can I get the QModelIndex of the first/last row to be passed on QItemSelection::contains( const QModelIndex & index ) ???

IndikaU
14th September 2011, 06:14
Hi,

I could manage to achieve what I expected. I'm putting the code here.

//Initial setup - 3 columns, 3 empty rows

p_TableFields = new QStandardItemModel(this);

p_TableFields->insertColumns(0, 3);
p_TableFields->insertRows(0, 3);

p_TableFields->setHeaderData(0, Qt::Horizontal, tr("Field Name"));
p_TableFields->setHeaderData(1, Qt::Horizontal, tr("Data Type"));
p_TableFields->setHeaderData(2, Qt::Horizontal, tr("Description"));

tblFields->setModel(p_TableFields);

connect(tblFields->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(OnSelect(const QItemSelection&, const QItemSelection&)));

//OnSelect
QModelIndexList RowsSelected = tblFields->selectionModel()->selectedRows();

//Check if first row selected
QModelIndex firstElement = p_TableFields->index(0, 0);
bool hasFirstRow = RowsSelected.contains(firstElement);

//Check if last row selected
QModelIndex lastElement = p_TableFields->index((p_TableFields->rowCount()-1), 0);
bool hasLastRow = RowsSelected.contains(lastElement);

btnMoveUp->setEnabled(!hasFirstRow);
btnMoveDown->setEnabled(!hasLastRow);

68486849