1

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?

Kolyunya
  • 5,973
  • 7
  • 46
  • 81

2 Answers2

5

The clicked(bool) signal is always false for non-checkable buttons. It will never emit true unless you set setCheckable(true), in which case it emits true when you check it, and false when you uncheck it. Non-checkable buttons cannot be checked (obviously), which is why the signal always emits false.

So in this case, simply provide your own slot where you manually toggle between setDisabled(true) and setDisabled(false). Or make the button checkable first with setCheckable(true) (it might even be more suitable in this case; just test it and see.)

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
0

QLineEdit does not have a method setDisabled according to the documentation

Try setReadOnly instead.

slosd
  • 3,224
  • 2
  • 21
  • 17
  • 1
    The documentation says that it has `19 public slots inherited from QWidget`, and `void setDisabled ( bool disable )` in particular. – Kolyunya Oct 25 '12 at 10:01
  • Sry, you are right. Is the button checkable? Otherwise the argument seems to be false all the time. – slosd Oct 25 '12 at 10:07
  • Nope, the button is not checkable... Seems like I should implement my own signal? – Kolyunya Oct 25 '12 at 10:09
  • Or you could extend `QLineEdit` and implement a `toggleDisabled()` that does not need any arguments. – slosd Oct 25 '12 at 10:21