2

I've got a QLineEdit field and a QPushButton. The button should be disabled as long the QLineEdit is empty.

How to do that?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
eco
  • 321
  • 2
  • 4
  • 11
  • 1
    Initially make the button disabled; and then on the EditField, once it accepts some input, have it modify the button - if there is text, it enables the button, if the user deleted all the text, disable the button. – dwanderson May 03 '16 at 14:23
  • 1
    You might want to check out [signals and slots in Pyside](https://wiki.qt.io/Signals_and_Slots_in_PySide). Your QEditLine emits a [textChanged signal](https://deptinfo-ensip.univ-poitiers.fr/ENS/pyside-docs/PySide/QtGui/QLineEdit.html#PySide.QtGui.PySide.QtGui.QLineEdit.textChanged) when ... well, its text changes, and you can then check what the current text is and set your button state accordingly. – Hans May 03 '16 at 14:40

2 Answers2

5

well, i'll just conclude what they said in the comments, some code like

self.btnButton.setDisable(True)
self.leInput.textChanged.connect(self.disableButton)
def disableButton(self):
    if len(self.leInput.text()) > 0:
        self.btnButton.setDisable(False)

and yes, the signals / function names are obvious, you need to check more on the docs / tutor

Jack Wu
  • 447
  • 4
  • 14
2

Here is a one-liner solution:

self.textBox.textChanged[str].connect(lambda: self.myBtn.setEnabled(self.textBox.text() != ""))

You still have to set the initial state of the button to False. You can do that in the declaration though. e.g.

self.myBtn = QtGui.QPushButton("My Button", enabled=False)
DerFaizio
  • 148
  • 7