What is the use of QApplication::translate() in window.setWindowTitle(QApplication::translate("toplevel", "Top-level widget")); QT C++

  c++, qt, qt5, qtwidgets

Hi i have just started learning QT C++. And so came across this example code.

#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWidget window;
    window.resize(320, 240);
    window.show();
    window.setWindowTitle(
        QApplication::translate("toplevel", "Top-level widget"));//whe have we used QApplication::translate?
    return app.exec();
}

Here we have passed as the first argument to setWindowTitle() member function the QApplication::translate("toplevel", "Top-level widget"). I have tried this using a normal const char * and it works. According to the documentation QApplication::translate() returns the translation text for sourceText, by querying the installed translation files. The translation files are searched from the most recently installed file back to the first installed file. My questions are:

  1. When should i use this function? I know that it returns QString and that’s why when i pass a string literal it works.
  2. What are translation files and what are their uses? Also it is written above that

translation files are searched from the most recently installed file

What does "install" mean here. I mean how can i install a translation file?

In another example, the context argument is same:

window.setWindowTitle
          (QApplication::translate("childwidget", "Child widget"));
    window.show();

    QPushButton *button = new QPushButton(
        QApplication::translate("childwidget", "Press me"), &window);

Here the first argument is same for both the widgets. What does this mean? Does having the same argument signify/mean something?

Source: Windows Questions C++

LEAVE A COMMENT