Gino
Gino

Reputation: 189

How to enable/disable button when QComboxBox and QLineEdit are activated

I cannot get a push-button enabled when both a text box is filled and a combox box item is selected.

I have the following code, which enables the button as soon as I enter text, but have no selected combobox item:

self.combo_box.currentIndexChanged[int].connect(self.showbutton)
self.textbox.textChanged.connect(self.showbutton)

def showbutton(self,index):
    self.enableNewButton.setEnabled((index != -1) and bool(self.textbox.text()))

So I then did a print(index) and I do see the proper index being printed when I select a combo box item; but I also see each character printed as well when I enter stuff in the text box.

I have no problems enabling buttons based on multiple text input boxes but this one is not working for me.

Upvotes: 1

Views: 818

Answers (1)

ekhumoro
ekhumoro

Reputation: 120578

The textChanged signal sends the current text, which cannot ever be equal to -1. So rather than using the parameters emitted by the signals, you should directly check both values via the widgets themselves, like so:

class Window(QtWidgets.QWidget):
    ...
    def showbutton(self):
        self.enableNewButton.setEnabled(
            self.combo_box.currentIndex() >= 0 and bool(self.textbox.text()))
    

Upvotes: 1

Related Questions