Sally
Sally

Reputation: 87

How to hide Qcheckbox when Qcombox box selects an item?

from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(762, 590)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.checkBox = QtWidgets.QCheckBox('box', self.centralwidget)
        self.checkBox.setGeometry(QtCore.QRect(150, 75, 181, 20))
        self.checkBox.setObjectName("checkBox")


        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(150,160,100,20))
        self.comboBox.addItem("Yes")
        self.comboBox.addItem("No")
        self.comboBox.setObjectName("comboBox")



        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "11"))

        MainWindow.show()

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

I have created a combobox with "Yes" and "No", I want to hide the checkbox when I select "No" in the combobox, Can anyone help?

I tried to creat a function that when self.comboBox.currentText == "Yes" , run self.checkBox.hide(), but it didn't work...

Upvotes: 0

Views: 1929

Answers (3)

eyllanesc
eyllanesc

Reputation: 244282

You have to use the currentTextChanged signal that notifies you if the QComboBox selection has been changed sending you the new text, then you should only compare it with the text and together with the setVisible() method fulfill your requirement.

    self.comboBox.currentTextChanged.connect(self.handle_current_text_changed)

def handle_current_text_changed(self, text):
    self.checkBox.setVisible(text == "Yes")

Upvotes: 2

Stan Ol
Stan Ol

Reputation: 1

If you want to hide the checkbox when the comboBox state is HIDE, for example, and unhide checkBox when the combobox state is UNHIDE, use an IF construction to catch the state of the combobox. Depending on the state, apply one or the other value to the checkbox:


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(762, 590)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.checkBox = QtWidgets.QCheckBox('box', self.centralwidget)
        self.checkBox.setGeometry(QtCore.QRect(150, 75, 181, 20))
        self.checkBox.setObjectName("checkBox")


        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(150,160,100,20))
        self.comboBox.addItem("UNHIDE")
        self.comboBox.addItem("HIDE")
        self.comboBox.setObjectName("comboBox")


        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.comboBox.currentTextChanged.connect(self.hidefunction) # code for connect to function below

    def hidefunction(self):
        text = str(self.comboBox.currentText()) 
        # this is the state of the current text in combobox. Below simple IF block.
        if text == "UNHIDE":
            self.checkBox.setHidden(False) 
        # its HIDE - your checkBox when comboBox changed his state 
        else:
            self.checkBox.setHidden(True) 
        # its HIDE your checkBox when comboBox changed his state 
    
           

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "11"))

        MainWindow.show()

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Upvotes: 0

JComputerScience
JComputerScience

Reputation: 165

Use signals and slots to do this. Capture the Combobox's editTextChanged signal with a slot hideCheckBox.

comboBox.currentTextChanged.connect(func)

In the function func simply setVisibility to false when the text is "NO" and true when text is "YES".

Upvotes: 1

Related Questions