Problem with disabling button
Hello,
I have a problem with disabling button. I will be very grateful if you give me any advice.
Let’s assume that we have a dialog which displays item’s list. Dialog has Ok and Cancel buttons. I want when user clicks one of the items of the list (selecting the item) the OK button will be enabled otherwise (deselecting the item) OK button will be disabled.
I could enable Ok button. To do this I connect void QAbstractItemView::clicked(const QModelIndex & index) signal with the slot which enables Ok button. But this signal emitted when index is valid.
How can I disable Ok button? How I can be known that user clicks outside the item?
Re: Problem with disabling button
Hi hasmik!
The solution is to use the item view's selection model. The automatic enabling/disabling of the button can be achieved with a slot which will be connected to the selection model's selectionChanged signal.
Code:
MyDialog
::MyDialog(QWidget* parent
){
connect(itemView->selectionModel(),SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&))
,this,SLOT(onSelectionChanged()));
}
MyDialog::onSelectionChanged()
{
btnOk->setEnabled(itemView->selectionModel()->hasSelection());
}
Re: Problem with disabling button