Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why don't I need to include QObject to get the tr function in main window?
Forum Updated to NodeBB v4.3 + New Features

Why don't I need to include QObject to get the tr function in main window?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 3.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • GuerrianG Offline
    GuerrianG Offline
    Guerrian
    wrote on last edited by Guerrian
    #1

    In "mainwindow.cpp" I just write "tr" to get the translation function. But in another file I need to include QObject and then use it like:

    #include <QObject>
    ...
    QObject::tr("Some text")
    

    Why is this?

    Linux Mint 18.3
    Qt 5.14.1
    Qt Creator 4.11.1

    mrjjM 1 Reply Last reply
    1
    • GuerrianG Guerrian

      In "mainwindow.cpp" I just write "tr" to get the translation function. But in another file I need to include QObject and then use it like:

      #include <QObject>
      ...
      QObject::tr("Some text")
      

      Why is this?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @Guerrian
      hi
      When inside a QObject based class method, its directly available.
      ( #include <QObject> is from base already,and the tr method can just be called )
      When inside class with non QObject base class/global function , you must can use
      it as any static method and call QObject::tr and include the header.
      its explained here
      http://doc.qt.io/qt-5/i18n-source-translation.html

      1 Reply Last reply
      4
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        in addition to @mrjj MainWindow class is inherited from QObject. tr() function is method of QObject. From the object oriented principles of inheritance, all the base class functions are available in derived class as well. Hence you don't need QObject::tr() inside the main window class. If you are code is any method of class & if the class is not inherited from Object you need to explicitly call the QObject::tr(...). Hope this clarifies. Also tr() is static function.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        3
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          In addition to what my fellows already wrote. You don't need your classes to derive from QObject to give them support for translation. See Translating Non-Qt Classes in the Qt documentation chapter @mrjj linked. This method is cleaner than having unrelated calls to static methods in your code.

          In the same document you can also see the use of QCoreApplication::translate for code that is outside any class. That again makes things cleaner and clearer.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          5

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved