Ho scritto una classe speciale QMessageBoxEx per questo problema.
// init once your button texts
QMessageBoxEx::setCustomTextForButton(QMessageBox::Yes, "Да");
QMessageBoxEx::setCustomTextForButton(QMessageBox::No, "Нет");
// example usage
if (QMessageBoxEx::question(this, "Внимание", "Ошибка", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
{
// OK
}
// header
class QMessageBoxEx : public QMessageBox
{
private:
static QMap<QMessageBox::StandardButton, QString> m_customButtonNames;
protected:
static void setCustomTextForButtons(QMessageBoxEx &msgBox);
public:
QMessageBoxEx(QWidget *parent);
static void setCustomTextForButton(QMessageBox::StandardButton button, const QString &text);
static QMessageBox::StandardButton critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = NoButton);
static QMessageBox::StandardButton information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = NoButton);
static QMessageBox::StandardButton question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No), QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
static QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = NoButton);
};
// implementation
QMap<QMessageBox::StandardButton, QString> QMessageBoxEx::m_customButtonNames;
void QMessageBoxEx::setCustomTextForButton(QMessageBox::StandardButton button, const QString &text)
{
if (m_customButtonNames.contains(button))
m_customButtonNames.erase(m_customButtonNames.find(button));
m_customButtonNames[button] = text;
}
void QMessageBoxEx::setCustomTextForButtons(QMessageBoxEx &msgBox)
{
if (m_customButtonNames.size())
{
QMessageBox::StandardButtons buttons = msgBox.standardButtons();
for (auto button : m_customButtonNames.keys())
{
if (buttons & button)
{
msgBox.setButtonText(button, m_customButtonNames[button]);
}
}
}
}
QMessageBox::StandardButton QMessageBoxEx::critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
QMessageBoxEx msgBox(parent);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle(title);
msgBox.setText(text);
msgBox.setStandardButtons(buttons);
msgBox.setDefaultButton(defaultButton);
setCustomTextForButtons(msgBox);
return (QMessageBox::StandardButton)msgBox.exec();
}
QMessageBox::StandardButton QMessageBoxEx::information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
QMessageBoxEx msgBox(parent);
msgBox.setIcon(QMessageBox::Information);
msgBox.setWindowTitle(title);
msgBox.setText(text);
msgBox.setStandardButtons(buttons);
msgBox.setDefaultButton(defaultButton);
setCustomTextForButtons(msgBox);
return (QMessageBox::StandardButton)msgBox.exec();
}
QMessageBox::StandardButton QMessageBoxEx::question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
QMessageBoxEx msgBox(parent);
msgBox.setIcon(QMessageBox::Question);
msgBox.setWindowTitle(title);
msgBox.setText(text);
msgBox.setStandardButtons(buttons);
msgBox.setDefaultButton(defaultButton);
setCustomTextForButtons(msgBox);
return (QMessageBox::StandardButton)msgBox.exec();
}
QMessageBox::StandardButton QMessageBoxEx::warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
QMessageBoxEx msgBox(parent);
msgBox.setIcon(QMessageBox::Warning);
msgBox.setWindowTitle(title);
msgBox.setText(text);
msgBox.setStandardButtons(buttons);
msgBox.setDefaultButton(defaultButton);
setCustomTextForButtons(msgBox);
return (QMessageBox::StandardButton)msgBox.exec();
}
QMessageBoxEx::QMessageBoxEx(QWidget *parent) : QMessageBox(parent)
{
}
Gist: https://gist.github.com/kleuter/81a75a50e60a75aa0370a66ededc0c31
Possibile duplicato http://stackoverflow.com/questions/18979062/qt-dynamic-translation-of-dialog-windows – Tarod