How to activate signals when required

Viewed 43

In this minimal reproducible example, I have a comboBox and a pushButton. I am trying to activate buttons on the basis of current text selected from the comboBox, but I can't able activate buttons when I tried to verify it first inside if elif else condition, how to activate right function on the basis of current text.

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

import sys

class MainWindow(QWidget):
    def __init__(self):
        super(QWidget, self).__init__()

        main_layout = QVBoxLayout(self)

        self.buttons = []

        # Works:
        self.combo = QComboBox()
        main_layout.addWidget(self.combo)
        self.combo.addItems(['PHC'])
        self.combo.addItems(['CHC'])
        self.combo.addItems(['HSC'])
        self.combo.addItems(['DH'])
        self.combo.addItems(['LSH'])
        # # Connecting comboBox to VerifyFType function
        self.combo.currentIndexChanged[str].connect(self.VerifyFType)

        self.button_2 = QPushButton('Validate', self)
        main_layout.addWidget(self.button_2)
        self.buttons.append(self.button_2)

    def VerifyFType(self):
        print("Entered VerifyFType")

        FType = self.combo.currentText()
        print(FType)

        if(FType == "PHC"):
            self.button_2.clicked.connect(lambda: self.PHC_Validate)
        elif(FType == "CHC"):
            self.button_2.clicked.connect(lambda: self.CHC_Validate)
        elif(FType == "DH"):
            self.button_2.clicked.connect(lambda: self.DH_Validate)
        elif(FType == "HSC"):
            self.button_2.clicked.connect(lambda: self.HSC_Validate)
        elif(FType == "LSH"):
            self.button_2.clicked.connect(lambda: self.LSH_Validate)
        else:
            "Nothing Matched , No such FType"

    
    def PHC_Validate(self):
        print('Entered PHC_Validate')

    def CHC_Validate(self):
        print('Entered CHC_Validate')

    def DH_Validate(self):
        print('Entered DH_Validate')

    def HSC_Validate(self):
        print('Entered HSC_Validate')

    def LSH_Validate(self):
        print('Entered LSH_Validate')


app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
1 Answers

Your logic is wrong since you seem to think that connecting the signal to another function will disconnect the signal from the previous function.

The solution is to invoke the appropriate function using the currentText of the QComboBox when the button is pressed.

class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.combo = QComboBox()
        self.combo.addItems(["PHC", "CHC", "HSC", "DH", "LSH"])

        self.button_2 = QPushButton("Validate", self)
        self.button_2.clicked.connect(self.handle_clicked)

        main_layout = QVBoxLayout(self)
        main_layout.addWidget(self.combo)
        main_layout.addWidget(self.button_2)

    def handle_clicked(self):
        FType = self.combo.currentText()
        if FType == "PHC":
            self.PHC_Validate()
        elif FType == "CHC":
            self.CHC_Validate()
        elif FType == "DH":
            self.DH_Validate()
        elif FType == "HSC":
            self.HSC_Validate()
        elif FType == "LSH":
            self.LSH_Validate()
        else:
            "Nothing Matched , No such FType"

    def PHC_Validate(self):
        print("Entered PHC_Validate")

    def CHC_Validate(self):
        print("Entered CHC_Validate")

    def DH_Validate(self):
        print("Entered DH_Validate")

    def HSC_Validate(self):
        print("Entered HSC_Validate")

    def LSH_Validate(self):
        print("Entered LSH_Validate")
Related