In my Qt project I’ve a QPushButton and a QLineEdit instances. And I want to set QLineEdit disabled when QPushButton is pressed.
I’ve written this code:
this->btn = new QPushButton(this);
this->txt = new QLineEdit(this);
QObject::connect(this->btn,SIGNAL(clicked(bool)),this->txt,SLOT(setDisabled(bool)));
which doesn’t work.
Can you help me please? What is my mistake?
The
clicked(bool)signal is alwaysfalsefor non-checkable buttons. It will never emittrueunless you setsetCheckable(true), in which case it emitstruewhen you check it, andfalsewhen you uncheck it. Non-checkable buttons cannot be checked (obviously), which is why the signal always emitsfalse.So in this case, simply provide your own slot where you manually toggle between
setDisabled(true)andsetDisabled(false). Or make the button checkable first withsetCheckable(true)(it might even be more suitable in this case; just test it and see.)