Problems with Open-Source Downloads read and

Unable to translate QAction text (empty string shown)

  • Hi,

    I have got a QtDesigner built application where I add a language menu at runtime (pretty much like in the examples).

    The menu should show the names of the languages in their native language (eg. English = "English", German = "Deutsch", Spanish = "Espanol", etc).

    The QActions are added to a QActionGroup at runtime in a createLanguageMenu() method. But action->setText() shows only empty strings...
    It works when I set the display text to the locale based (english) name of the respective language.

    How can I get those entries to show the correct display text (native name)?
    And is there anything special needed to display "exotic" language names as Chinese or Hebrew in their native writing?

    If you need more information, I'll post as required...

    Thank you,

  • Moderators

    there are many possible mistakes.

    Please post some code (especially where you load the translator and retranslate the gui on a translator change).

  • Thank you for responding.

    Qt version is 4.7.0.
    I have used the following article as a guideline in addition to the samples and tutorials from this site:
    "Dynamic Language Switching":

    I create the language menu in my MainWindow class (derived from QMainWindow):
    @void MainWindow::createLanguageMenu()
    QAction *action;
    QString locale;

    // get reference to languages menu entry
    QMenu *menuLang = menuBar()->findChild<QMenu *>("menuLanguages");
    // create new action group
    QActionGroup *langGroup = new QActionGroup(menuLang);
    // attach event handler
    connect(langGroup, SIGNAL(triggered(QAction *)), this, SLOT(slotLanguageChanged(QAction *)));
    // read files from directory
    QDir dir(qApp->applicationDirPath().append("/languages"));
    QStringList fileList = dir.entryList(QStringList("*.qm"));
    // iterate through list and create an entry for every language file found
    QTranslator translator;
    for (int i = 0; i < fileList.size(); i++)
        QString displayName = translator.translate("MainWindow", "languageDisplayName");
        // extract locale from file name
        locale = fileList[i];
        locale.remove(0, locale.lastIndexOf('_') + 1);
        action = createLocaleAction(locale, i + 1, displayName);
        if (m_currLang == locale)


    The actions are created in a seperate function:
    @QAction* MainWindow::createLocaleAction(const QString &locale, int pos, const QString &displayName)
    QString lang = QLocale::languageToString(QLocale(locale).language());
    //QAction *action = new QAction(displayName, this);
    QAction *action = new QAction(lang, this);
    action->setStatusTip(QString(tr("Switch to ")).append(lang));
    action->setToolTip(QString(tr("Switch to ")).append(lang));

    return action;


    However, when I use the "displayName" parameter (which should represent the native language name) to set the action text, nothing is shown.

    When I use the "lang" String extracted from a QLocale object, texts are displayed correctly:

Log in to reply