0

I'm attempting to create a Login System type dialog box for practice using PyQt5 (I'm quite new to the module) and i'm trying to give the user the ability to click (Ok, Cancel, Apply) as the buttons underneath inputs boxes for Username / Password, but i'm not sure how I can actually get the apply button to work. I have buttons.accepted.connect(*method*) and buttons.rejected.connect(*method*) but I don't know how to specify the pressing of the accept button. I have tried using buttons.clicked(dlgButtons[0] (Which is where the button is stored) but it just gives me an error.

The code below is my declaration of the buttons if that helps. Thanks

buttons = qt.QDialogButtonBox()
        dlgButtons = (qt.QDialogButtonBox.Apply, qt.QDialogButtonBox.Ok, qt.QDialogButtonBox.Cancel)
        buttons.setStandardButtons(
        dlgButtons[0] | dlgButtons[1] | dlgButtons[2]
        )

2 Answers 2

2

One possible solution might look like this:

from PyQt5.QtWidgets import *

class ModelessDialog(QDialog):
    def __init__(self, part, threshold, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Baseline")
        self.setGeometry(800, 275, 300, 200)
        self.part = part
        self.threshold = threshold
        self.threshNew = 4.4
        
        label    = QLabel("Part            : {}\nThreshold   : {}".format(
                                                self.part, self.threshold))
        self.label2 = QLabel("ThreshNew : {:,.2f}".format(self.threshNew))
        
        self.spinBox = QDoubleSpinBox()
        self.spinBox.setMinimum(-2.3)
        self.spinBox.setMaximum(99)
        self.spinBox.setValue(self.threshNew)
        self.spinBox.setSingleStep(0.02)
        self.spinBox.valueChanged.connect(self.valueChang)
        
        buttonBox = QDialogButtonBox(
            QDialogButtonBox.Ok 
            | QDialogButtonBox.Cancel
            | QDialogButtonBox.Apply)

        layout = QVBoxLayout()            
        layout.addWidget(label)
        layout.addWidget(self.label2)
        layout.addWidget(self.spinBox)
        layout.addWidget(buttonBox)
        self.resize(300, 200)  
        self.setLayout(layout)                                 

        okBtn = buttonBox.button(QDialogButtonBox.Ok) 
        okBtn.clicked.connect(self._okBtn)

        cancelBtn = buttonBox.button(QDialogButtonBox.Cancel)
        cancelBtn.clicked.connect(self.reject)   

        applyBtn = buttonBox.button(QDialogButtonBox.Apply)       # +++
        applyBtn.clicked.connect(self._apply)                     # +++

    def _apply(self):                                             # +++
        print('Hello Apply')    

    def _okBtn(self):
        print("""
            Part      : {}
            Threshold : {}
            ThreshNew : {:,.2f}""".format(
                self.part, self.threshold, self.spinBox.value()))
        
    def valueChang(self):
        self.label2.setText("ThreshNew : {:,.2f}".format(self.spinBox.value()))
        

class Window(QWidget):
    def __init__(self):
        super().__init__()
        label  = QLabel('Hello Dialog', self)
        button = QPushButton('Open Dialog', self)
        button.clicked.connect(self.showDialog)
        
        layout = QVBoxLayout()
        layout.addWidget(label)
        layout.addWidget(button)
        self.setLayout(layout)        

    def showDialog(self):
        self.dialog = ModelessDialog(2, 55.77, self)
        self.dialog.show()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    win = Window()
    win.resize(300, 200)
    win.show()
    sys.exit(app.exec_())

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

But the problem is I don't know how to assign a function to the apply button, and i'm quite new to this module and can't really tell where you have done that. EDIT: Nevermind, i'm just an idiot and didn't see that I could scroll down, ignore me and thank you for the answer
@Nyaywraith I marked the lines for you # +++
What does the spinbox has to do, man? What happened to "keep the code minimal and reproducible?"
2

What you are storing in the dlgButtons is just a list of enums, specifically the StandardButton enum, which is a list of identifiers for the buttons, they are not the "actual" buttons.

Also, you cannot use the clicked signal like this:

 buttons.clicked(dlgButtons[0])

That will generate a crash, as signals are not callable. The argument of the clicked() signal is what will be received from the slot, which means that if you connect a function to that signal, the function will receive the clicked button:

        buttons.clicked.connect(self.buttonsClicked)

    def buttonsClicked(self, button):
        print(button.text())

The above will print the text of the clicked button (Ok, Apply, Cancel, or their equivalent localized text).

What you're looking for is to connect to the clicked signals of the actual buttons, and you can get the individual reference to each button by using the button() function:

applyButton = buttons.button(qt.QDialogButtonBox.Apply)
applyButton.clicked.connect(self.applyFunction)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.