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
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