18

I want to use QMessageBox.Question for the icon. But i want to change the text of standard buttons. I do not want the text of buttons to be the "Yes" and "No". I want them to be "Evet" and "Iptal". Here my codes.

choice = QtGui.QMessageBox.question(self, 'Kaydet!',
                                    'Kaydetmek İstediğinize Emin Misiniz?',
                                    QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
Taylan
  • 736
  • 1
  • 5
  • 14
  • 1
    Actually, now i overcome this problem like this choice = QtGui.QMessageBox.question(self, "Kaydet!", "Kaydetmek İstediğinize Emin Misiniz?", "Evet", button1Text='İptal', defaultButtonNumber=0, escapeButtonNumber=-1) if choice == 0: self.frame_11.show() else: pass – Taylan Mar 09 '16 at 09:42
  • That form of QMesageBox::question is both undocumented and commented as obsolete. Still exits as of Qt 5.15.4 but no Guarantee of this even for the next bug fix release much less Qt6 which is now released. – wheredidthatnamecomefrom Mar 15 '23 at 04:03

2 Answers2

36

To do so you need to create an instance of a QMessageBox and manually change the text of the buttons:

box = QtGui.QMessageBox()
box.setIcon(QtGui.QMessageBox.Question)
box.setWindowTitle('Kaydet!')
box.setText('Kaydetmek İstediğinize Emin Misiniz?')
box.setStandardButtons(QtGui.QMessageBox.Yes|QtGui.QMessageBox.No)
buttonY = box.button(QtGui.QMessageBox.Yes)
buttonY.setText('Evet')
buttonN = box.button(QtGui.QMessageBox.No)
buttonN.setText('Iptal')
box.exec_()

if box.clickedButton() == buttonY:
    # YES pressed
elif box.clickedButton() == buttonN:
    # NO pressed
Daniele Pantaleone
  • 2,657
  • 22
  • 33
8

Qt provides translations for all the built-in strings it uses in its libraries. You just need to install a translator for the current locale:

app = QtGui.QApplication(sys.argv)

translator = QtCore.QTranslator(app)
locale = QtCore.QLocale.system().name()
path = QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.TranslationsPath)
translator.load('qt_%s' % locale, path)
app.installTranslator(translator)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • You might also want to check other files than 'qt_%s'. For example my Qt installation had empty qt_pl.qm, which failed to load, but qtbase_pl.qm had all translations I wanted. There are other types of translation files in `TranslationsPath` directory as well. – Michał Góral Aug 30 '17 at 06:22