How to change a QMenu text from english to russian by clicking a button

Please consider we have a menu which has text set "MyMenu" and I want to change the menu text by clicking a button in the same widget from "MyMenu" to "МойМеню". Could you bring a code snippet please for that operation?

Answers


Have a look at "Dynamic Translation", http://doc.qt.io/qt-5/internationalization.html

void MyWidget::changeEvent(QEvent *event)
{
    if (e->type() == QEvent::LanguageChange)
    {
        titleLabel->setText(tr("Document Title"));
        ...
        okPushButton->setText(tr("&OK"));

        // You could also use : retranslateUi(QWidget*);
    } 
    else
    {
        QWidget::changeEvent(event);
    }
}

This will be helpful to you as well : http://doc.qt.io/qt-5/qcoreapplication.html#installTranslator

Basically, when you call : qApp->installTranslator(MyAppTranslator) it will create a QEvent::LanguageChange.

So, provide a simple QComboBox with English/Russian, and when the selected language changes, call qApp->installTranslator(MyAppTranslator);. Then make sure your buttons are properly set up in changeEvent, and that's it !

Hope it helps a bit !


int main(int argc, char *argv[])
{

    QApplication app(argc, argv);

    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));//this is the solution
    .............
}

And in the code you can dynamically change the strings if you set them from the begining by using tr() function [tr("your text")].


Need Your Help

error (261, -1003) android error in audio play

android audio

I am trying to play an audio on click of an Image. When I run the app and click on the Image I get below error in the console: