pythonpyqt5requiredfieldvalidator

How to make pyqt5 multiple input widgets mandatory to enter something?


I want to make some input widgets mandatory in pyqt5. I need to give message If user missed the mandatory inputs. In this scenario i want to set mandatory input widgets as self.comboComboBox and self.spinSpinBox are definitely mandatory and either self.plainTextEdit and self.doube_one_spinDoubleSpinBox or self.lineLineEdit and self.double_two_spinDoubleSpinBox are mandatory. Below is my tried example:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(QtWidgets.QMainWindow):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(334, 417)
        MainWindow.setMinimumSize(QtCore.QSize(334, 417))
        MainWindow.setMaximumSize(QtCore.QSize(334, 417))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(20, 10, 291, 351))
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.pushButton = QtWidgets.QPushButton(self.frame)
        self.pushButton.setGeometry(QtCore.QRect(90, 320, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.lineLabel = QtWidgets.QLabel(self.frame)
        self.lineLabel.setGeometry(QtCore.QRect(10, 250, 41, 20))
        self.lineLabel.setObjectName("lineLabel")
        self.lineLineEdit = QtWidgets.QLineEdit(self.frame)
        self.lineLineEdit.setGeometry(QtCore.QRect(100, 245, 133, 20))
        self.lineLineEdit.setObjectName("lineLineEdit")
        self.comboLabel = QtWidgets.QLabel(self.frame)
        self.comboLabel.setGeometry(QtCore.QRect(11, 17, 31, 16))
        self.comboLabel.setObjectName("comboLabel")
        self.comboComboBox = QtWidgets.QComboBox(self.frame)
        self.comboComboBox.setGeometry(QtCore.QRect(98, 17, 121, 20))
        self.comboComboBox.setObjectName("comboComboBox")
        self.spinLabel = QtWidgets.QLabel(self.frame)
        self.spinLabel.setGeometry(QtCore.QRect(11, 43, 19, 16))
        self.spinLabel.setObjectName("spinLabel")
        self.spinSpinBox = QtWidgets.QSpinBox(self.frame)
        self.spinSpinBox.setGeometry(QtCore.QRect(98, 43, 121, 20))
        self.spinSpinBox.setObjectName("spinSpinBox")
        self.doube_one_spinLabel = QtWidgets.QLabel(self.frame)
        self.doube_one_spinLabel.setGeometry(QtCore.QRect(13, 180, 79, 21))
        self.doube_one_spinLabel.setObjectName("doube_one_spinLabel")
        self.doube_one_spinDoubleSpinBox = QtWidgets.QDoubleSpinBox(self.frame)
        self.doube_one_spinDoubleSpinBox.setGeometry(QtCore.QRect(100, 180, 131, 20))
        self.doube_one_spinDoubleSpinBox.setObjectName("doube_one_spinDoubleSpinBox")
        self.double_two_spinLabel = QtWidgets.QLabel(self.frame)
        self.double_two_spinLabel.setGeometry(QtCore.QRect(10, 275, 81, 16))
        self.double_two_spinLabel.setObjectName("double_two_spinLabel")
        self.double_two_spinDoubleSpinBox = QtWidgets.QDoubleSpinBox(self.frame)
        self.double_two_spinDoubleSpinBox.setGeometry(QtCore.QRect(100, 275, 131, 20))
        self.double_two_spinDoubleSpinBox.setObjectName("double_two_spinDoubleSpinBox")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(self.frame)
        self.plainTextEdit.setGeometry(QtCore.QRect(100, 110, 131, 61))
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.label = QtWidgets.QLabel(self.frame)
        self.label.setGeometry(QtCore.QRect(10, 130, 43, 16))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 334, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        #
        self.pushButton.clicked.connect(self.okay)
        self.comboComboBox.addItems(["", "One", "Two"])

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Okay"))
        self.lineLabel.setText(_translate("MainWindow", "line"))
        self.comboLabel.setText(_translate("MainWindow", "combo"))
        self.spinLabel.setText(_translate("MainWindow", "spin"))
        self.doube_one_spinLabel.setText(_translate("MainWindow", "doube_one_spin"))
        self.double_two_spinLabel.setText(_translate("MainWindow", "double_two_spin"))
        self.label.setText(_translate("MainWindow", "Plain Edit"))

    def okay(self):
        if self.comboComboBox.currentText() !='' and self.spinSpinBox.value() !=0 and\
           (self.plainTextEdit.toPlainText() !='' and self.doube_one_spinDoubleSpinBox.value() !=0.00) or\
           (self.lineLineEdit.text() !='' and self.double_two_spinDoubleSpinBox.value() != 0.00):
            QtWidgets.QMessageBox.about(self, "Information", "Done.")

        else:
            QtWidgets.QMessageBox.about(self, "Information", "Required Fields are Mandatory.")


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_())

In my example it is printing "OKAY" if i filled something in self.lineLineEdit and self.double_two_spinDoubleSpinBox even self.comboComboBox and self.spinSpinBox are empty. How to solve this? or Is there any way ? Its very useful if it is simple and easy. How to do?

Update:

My requirement is that i have 6 widgets. pair1(self.comboComboBox, self.spinSpinBox), pair2(self.plainTextEdit, self.doube_one_spinDoubleSpinBox), pair3(self.lineLineEdit, self.double_two_spinDoubleSpinBox). I want to set mandatory feilds as pair1 is mandatory. And (either pair2 or pair 3) is mandatory along with pair1.


Solution

  • As per the comments, I Got the the missing points and I regrouped the combinations. Now It is working as below.

            if self.comboComboBox.currentText() !='' and self.spinSpinBox.value() !=0 and\
               ((self.plainTextEdit.toPlainText() !='' and self.doube_one_spinDoubleSpinBox.value() !=0.00) or\
               (self.lineLineEdit.text() !='' and self.double_two_spinDoubleSpinBox.value() != 0.00)):
                QtWidgets.QMessageBox.about(self, "Information", "Done.")
    
            else:
                QtWidgets.QMessageBox.about(self, "Information", "Required Fields are Mandatory.")