nicole86
nicole86

Reputation: 43

Change CancelButtonText of QInputDialog

I want to overwrite/change the text of the Cancel button of a QInputDialog that I use for a password input. I tried the following, but the new text does not show up during the getText. The logging shows that the new text was saved inside.

Does anybody know, what I'm doing wrong?

Thanks in advance.

caption_cancel = 'Cancel and go to previous mode'

input_password_dialog = QtWidgets.QInputDialog(self)
input_password_dialog.setInputMode(QtWidgets.QInputDialog.TextInput)
input_password_dialog.setOption(QtWidgets.QInputDialog.UsePlainTextEditForTextInput)

logging.debug('cancelButtonText = ' + input_password_dialog.cancelButtonText())  # 'Cancel'

input_password_dialog.setCancelButtonText(caption_cancel)

logging.debug('cancelButtonText = ' + input_password_dialog.cancelButtonText())  # 'Cancel and go to previous mode'

password, ok = input_password_dialog.getText(self, 'Authentification', 'Password',
                                             echo=QtWidgets.QLineEdit.Password)  # just shows 'Cancel"

Upvotes: 1

Views: 471

Answers (1)

eyllanesc
eyllanesc

Reputation: 244282

You have 2 errors:

  • QInputDialog::getText() is a static method so it will not use the input_password_dialog object, so you must directly set the properties such as the title, label, etc. to the input_password_dialog object, or try to apply a hack to get the QInputDialog created in the static method .

  • If you set QInputDialog.TextInput and QInputDialog.UsePlainTextEditForTextInput then the dots will not be displayed instead of text as a password entry.

Considering the above, the solution is:

caption_cancel = "Cancel and go to previous mode"

input_password_dialog = QtWidgets.QInputDialog(self)
# input_password_dialog.setInputMode(QtWidgets.QInputDialog.TextInput)
# input_password_dialog.setOption(QtWidgets.QInputDialog.UsePlainTextEditForTextInput)
input_password_dialog.setCancelButtonText(caption_cancel)
input_password_dialog.setWindowTitle("Authentification")
input_password_dialog.setLabelText("Password")
input_password_dialog.setTextEchoMode(QtWidgets.QLineEdit.Password)

if input_password_dialog.exec_() == QtWidgets.QDialog.Accepted:
    password = input_password_dialog.textValue()
    print(password)

Upvotes: 2

Related Questions