QInputDialog中如何更改默认文本及OK和Cancel按钮的功能

How to change the default text and the function of OK and Cancel button in QInputDialog

我在我的代码中使用这一行来弹出 QInputDialog 并接受用户的输入。

但我想更改此弹出对话框上的按钮

def add(self):
    text, ok = QInputDialog.getText(self, " ", "Enter Value")

    if text == "" or text.isdigit() == False:
        print("Enter valid input")
        self.alert()
    else:
        print("ok value")
        self.ui.label_result.setText(str(text))

使用静态方法QInputDialog::getText() it is difficult to modify the text of the buttons, so instead of using an object of that class and using the methods setOkButtonText() and setCancelButtonText():

def add(self):
    dialog = QInputDialog(self)
    dialog.setWindowTitle(" ")
    dialog.setLabelText("Enter Value")
    dialog.setOkButtonText("Foo")
    dialog.setCancelButtonText("Bar")
    if dialog.exec_() == QDialog.Accepted:
        text = dialog.textValue()
        if text == "" or not text.isdigit():
            print("Enter valid input")
            self.alert()
        else:
            print("ok value")
            self.ui.label_result.setText(str(text))
    else:
        print("canceled")