
Originally Posted by
unix7777
i have a table and have 4 buttons.One for add new row, one for delete a row and one that edit the row.The 4th button is for selecting the row.When row is selected the data from the selected row is displayed on another widget.The problem is that is the user click on the select button before select a row the program crashes.This is why i want to make this check, i want the select button to be enabled only when some row is selected and when is not to be disabled.
Then your connect from before:
this, SLOT(setBool()));
connect(ui->tableView_clients->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
this, SLOT(setBool()));
To copy to clipboard, switch view to plain text mode
should be right, but the slot should look like this:
void Clients::setBool()
{
ui->pushButton_select->setEnabled(ui->tableView_clients->currentIndex().isValid());
}
void Clients::setBool()
{
ui->pushButton_select->setEnabled(ui->tableView_clients->currentIndex().isValid());
}
To copy to clipboard, switch view to plain text mode
so the button gets enabled when there is a selection and disabled when there is none.
I’m assuming you’ve called (or set in Designer) ui->tableView_clients->setSelectionBehavior(QAbstractItemView::SelectRows) so that only rows can be selected. (If the user is supposed to be able to select either individual items or full rows and the problem is distinguishing specifically when a full row is selected, that would be a bit different.)
Be sure you’re doing the connect after you’ve attached the model and set all the other characteristics of tableView_clients, to be certain you don’t inadvertently do something that replaces the selectionModel after the connect. Also don’t forget that the signal will only reach the slot for changes that occur after the connect; so you might need to call the slot explicitly just before or after the connect to be sure everything starts off right:
setBool();
connect(ui
->tableView_clients
->selectionModel
(),
SIGNAL(currentRowChanged
(QModelIndex,
QModelIndex)),
this,
SLOT(setBool
()));
setBool();
connect(ui->tableView_clients->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(setBool()));
To copy to clipboard, switch view to plain text mode
Bookmarks