PDA

View Full Version : Problem with disabling button



hasmik
11th July 2009, 15:11
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?

gsmiko
11th July 2009, 16:42
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.

MyDialog::MyDialog(QWidget* parent)
: QDialog(parent)
{
connect(itemView->selectionModel(),SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&))
,this,SLOT(onSelectionChanged()));
}

MyDialog::onSelectionChanged()
{
btnOk->setEnabled(itemView->selectionModel()->hasSelection());
}

hasmik
11th July 2009, 19:19
Thank you very much:).