开发者

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

开发者 https://www.devze.com 2022-12-23 15:08 出处:网络
Please con开发者_开发问答sider 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

Please con开发者_开发问答sider 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?


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")].

0

精彩评论

暂无评论...
验证码 换一张
取 消