I have a QTableView
and a button. When the row is selected from QTableView
I want button to enable and if the row is not selected then it should disable. can anyone tell how doing it
Asked
Active
Viewed 744 times
2

Chaya
- 137
- 11
1 Answers
2
Use table selection model to handle selection changed signal
then test the selection size.
Example:
QItemSelectionModel *poSelectedFiles =
m_poTableView->selectionModel();
connect(poSelectedFiles, &QItemSelectionModel::selectionChanged,
this, &MainWindow::SlotSelectionChanged);
void MainWindow::SlotSelectionChanged(const QItemSelection & oSelected, const QItemSelection & oDeselected)
{
const int iSelectedItems =
m_poTableView->selectionModel()->selectedIndexes().size();
bool bEnabled = (iSelectedItems > 0) ? true : false;
// Enable button when row is selected.
ui->poMyButton->setEnabled(bEnabled);
}

Simon
- 1,522
- 2
- 12
- 24