Convert standard button to QDialogButtonBox

I am using pyqt and constructor. I translated all the lines in my application using self.tr () + pylupdate4 and lrelease

here is the code snippet in my main ():

app = QtGui.QApplication(sys.argv)
app.setApplicationName('Mental Calculation')

# initialize locale and load translation files if available
locale = QtCore.QLocale()
LOCALENAME = str(locale.system().name())
translator = QtCore.QTranslator()
translator.load("mentalcalculation_%s" % LOCALENAME)
app.installTranslator(translator)

I am using QDialogButtonBox in QDialog with QtGui.QDialogButtonBox.Cancel and QtGui.QDialogButtonBox.Ok

and the lines in these buttons are not translated. because pylupdate4 selects any row for them.

Did I skip the configuration step in my application so that they are translated? I do not understand how the string for the standard QDialogButtonBox buttons should be translated and cannot find a document about it.

+3
source share
6 answers

Use the following method to set the button text:

buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Ok"));
buttonBox->button(QDialogButtonBox::Cancel)->setText(tr("Cancel"));

lupdate.ts , , .

+8

QT , :

translator.load("qt_" + QLocale::system().name(),QLibraryInfo::location(QLibraryInfo::TranslationsPath));

( ) , , , .

+3

- , : http://qt.nokia.com/developer/faqs/faq.2008-05-06.6265952148 http://qt.nokia.com/developer/faqs/705 p >

, , , qt_fr.qm, QTDIR/translations (/usr/share/qt/translations)

translator.load("qt_%s" % LOCALENAME)

qt _ *. qm QTDIR/translations, .

. . , QDailogButtonBox.

. .

+2

2 :

app = QtGui.QApplication(sys.argv)

translator_my = QtCore.QTranslator()
translator_my.load('i18n/i18n_' + QtCore.QLocale.system().name() + '.qm')
#translator_my.load('i18n/i18n_ru_Ru.qm')
app.installTranslator(translator_my)

translator_qt = QtCore.QTranslator()
translator_qt.load('i18n/qt_' + QtCore.QLocale.system().name()[:2] + '.qm') 
#translator_qt.load('i18n/qt_ru.qm')
app.installTranslator(translator_qt)

myApp = MyMainWindow()
myApp.show()
sys.exit(app.exec_())

i18n/i18n_ru_Ru.qm - i18n, qt_ru.qm usr/share/qt4/translations ( ).

+2

docs . , , , , , , .

0

. , , .

The problem is that Qtranslator cannot translate this QDialogButtonBox button because there is no line to translate. therefore it must be qt that does this work domestically. or with some mechanism that I don’t know about.

here is another piece of code generated by pyuic4

self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.gridLayout.addWidget(self.buttonBox, 8, 0, 1, 1)

As a line, QtGui.QDialogButtonBox.Cancel can be translated if there is no line in the code.

Is it because I am not creating mainwindow and only use QDialog ??

I can not comment on the answer of swanson!

-1
source

Source: https://habr.com/ru/post/1734770/


All Articles