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. all tr() which aren't prefixed with QObject doesn't work

all tr() which aren't prefixed with QObject doesn't work

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 300 Views 1 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.
  • A Offline
    A Offline
    AlexLuya
    wrote on last edited by
    #1

    in a QT(C++) based GUI,all .hpp files have been marked with Macro Q_OBJECT,corresponded .cpp files have many tr("keys to translate"),and these lines has been put in CMakeLists.txt

    file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/*.cpp" "${PROJECT_SOURCE_DIR}/shonDyCommon/*.cpp")
    file(GLOB_RECURSE UI_FILES "${PROJECT_SOURCE_DIR}/src/*.ui")
    file(GLOB_RECURSE HEADER_FILES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/*.hpp" "${PROJECT_SOURCE_DIR}/src/*.h" "${PROJECT_SOURCE_DIR}/shonDyCommon/*.hpp")
    
    set(TRANS_QRC ${CMAKE_CURRENT_BINARY_DIR}/trans_resources.qrc)
    configure_file(
            ${PROJECT_SOURCE_DIR}/src/resource/trans_resources.qrc.in
            ${TRANS_QRC}
            COPYONLY
    )
    set(TS_FILES
            ${PROJECT_SOURCE_DIR}/src/resource/translate/shonDy_zh_CN.ts
            ${PROJECT_SOURCE_DIR}/src/resource/translate/shonDy_en_US.ts
            )
    set_source_files_properties(${TS_FILES} PROPERTIES OUTPUT_LOCATION ${PROJECT_BINARY_DIR})
    qt5_create_translation(QM_FILES ${TS_FILES} ${SRC_FILES} ${UI_FILES} ${HEADER_FILES})
    
    qt5_add_big_resources(GUI_RESOURCES "${PROJECT_SOURCE_DIR}/src/resource/resources.qrc")
    

    All keys got generated in .ts file after compilation,no matter it comes from .ui file or .cpp file.

    When GUI opened,all keys which come from .ui files or QObject:tr("") got translated correctly,but all keys come from .cpp with tr("") which are not prefixed with QObject:: don't got translated.

    If picking any tr("specificKey") in a .cpp,prefixing it like this: QObject::tr("specificKey"),recompiling without any extra efforts,and open it again,this key got translated correctly.

    Question is:

    Why don't the keys in .cpp got translated?

    Christian EhrlicherC 1 Reply Last reply
    0
    • A AlexLuya

      in a QT(C++) based GUI,all .hpp files have been marked with Macro Q_OBJECT,corresponded .cpp files have many tr("keys to translate"),and these lines has been put in CMakeLists.txt

      file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/*.cpp" "${PROJECT_SOURCE_DIR}/shonDyCommon/*.cpp")
      file(GLOB_RECURSE UI_FILES "${PROJECT_SOURCE_DIR}/src/*.ui")
      file(GLOB_RECURSE HEADER_FILES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/*.hpp" "${PROJECT_SOURCE_DIR}/src/*.h" "${PROJECT_SOURCE_DIR}/shonDyCommon/*.hpp")
      
      set(TRANS_QRC ${CMAKE_CURRENT_BINARY_DIR}/trans_resources.qrc)
      configure_file(
              ${PROJECT_SOURCE_DIR}/src/resource/trans_resources.qrc.in
              ${TRANS_QRC}
              COPYONLY
      )
      set(TS_FILES
              ${PROJECT_SOURCE_DIR}/src/resource/translate/shonDy_zh_CN.ts
              ${PROJECT_SOURCE_DIR}/src/resource/translate/shonDy_en_US.ts
              )
      set_source_files_properties(${TS_FILES} PROPERTIES OUTPUT_LOCATION ${PROJECT_BINARY_DIR})
      qt5_create_translation(QM_FILES ${TS_FILES} ${SRC_FILES} ${UI_FILES} ${HEADER_FILES})
      
      qt5_add_big_resources(GUI_RESOURCES "${PROJECT_SOURCE_DIR}/src/resource/resources.qrc")
      

      All keys got generated in .ts file after compilation,no matter it comes from .ui file or .cpp file.

      When GUI opened,all keys which come from .ui files or QObject:tr("") got translated correctly,but all keys come from .cpp with tr("") which are not prefixed with QObject:: don't got translated.

      If picking any tr("specificKey") in a .cpp,prefixing it like this: QObject::tr("specificKey"),recompiling without any extra efforts,and open it again,this key got translated correctly.

      Question is:

      Why don't the keys in .cpp got translated?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Works fine for me. Please provide a minimal, compilable example to reproduce the problem - a simple main.cpp showing a QLabel should be enough. No need for a CMakeLists.txt

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Works fine for me. Please provide a minimal, compilable example to reproduce the problem - a simple main.cpp showing a QLabel should be enough. No need for a CMakeLists.txt

        A Offline
        A Offline
        Alex Luya
        wrote on last edited by Alex Luya
        #3

        @Christian-Ehrlicher The reuslt is shown like this:
        Selection_032.png

        And all codes are listed as blow,including .ui and CMakeLists.txt

        main.cpp

        #include "shonDyMainWindow.h"
        #include <QApplication>
        
        int main(int argc, char** argv)
        {
            using namespace shonCloud;
        
            QApplication application(argc, argv);
        
            shonDyMainWindow mainWindow;
            mainWindow.show();
            return application.exec();
        }
        

        shonDyMainWindow.h

        #pragma once
        #include <QMainWindow>
        
        
        namespace Ui
        {
        class shonDyMainWindow;
        }
        namespace shonCloud
        {
        
        class shonDyMainWindow : public QMainWindow
        {
            Q_OBJECT
        
           public:
            explicit shonDyMainWindow(QWidget *parent = nullptr);
        
           private:
            Ui::shonDyMainWindow *ui;
        };
        
        }  // namespace shonCloud
        

        shonDyMainWindow.cpp

        #include "shonDyMainWindow.h"
        #include "ui_shonDyMainWindow.h"
        #include <QTranslator>
        #include <QPushButton>
        
        namespace shonCloud
        {
        shonDyMainWindow::shonDyMainWindow(QWidget *parent)
            : ui(new Ui::shonDyMainWindow)
        {
            ui->setupUi(this);
            auto pb=new QPushButton(tr("Add by tr"),ui->centralwidget);
            ui->verticalLayout->addWidget(pb);
            auto translator = new QTranslator;
            translator->load(R"(shonDy_zh_CN.qm)");
            QCoreApplication::instance()->installTranslator(translator);
            ui->pushButton->setText(tr("Hello World"));
        }
        
        }  // namespace shonCloud
        

        shonDyMainWindow.ui

        <?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>shonDyMainWindow</class>
         <widget class="QMainWindow" name="shonDyMainWindow">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>1156</width>
            <height>765</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>MainWindow</string>
          </property>
          <widget class="QWidget" name="centralwidget">
           <layout class="QVBoxLayout" name="verticalLayout">
            <item>
             <widget class="QPushButton" name="pushButton">
              <property name="font">
               <font>
                <pointsize>19</pointsize>
               </font>
              </property>
              <property name="text">
               <string>Clicked Me</string>
              </property>
             </widget>
            </item>
           </layout>
          </widget>
          <widget class="QMenuBar" name="menubar">
           <property name="geometry">
            <rect>
             <x>0</x>
             <y>0</y>
             <width>1156</width>
             <height>18</height>
            </rect>
           </property>
           <widget class="QMenu" name="menuFile">
            <property name="title">
             <string>File</string>
            </property>
            <addaction name="separator"/>
           </widget>
           <addaction name="menuFile"/>
          </widget>
         </widget>
         <resources/>
         <connections/>
        </ui>
        

        shonDy_zh_CN.ts

        <?xml version="1.0" encoding="utf-8"?>
        <!DOCTYPE TS>
        <TS version="2.1" language="zh_CN">
        <context>
            <name>shonCloud::shonDyMainWindow</name>
            <message>
                <location filename="src/shonDyMainWindow.cpp" line="14"/>
                <source>Add by tr</source>
                <translation type="unfinished">在CPP中添加</translation>
            </message>
            <message>
                <location filename="src/shonDyMainWindow.cpp" line="19"/>
                <source>Hello World</source>
                <translation type="unfinished">你好,世界</translation>
            </message>
        </context>
        <context>
            <name>shonDyMainWindow</name>
            <message>
                <location filename="src/shonDyMainWindow.ui" line="14"/>
                <source>MainWindow</source>
                <translation type="unfinished"></translation>
            </message>
            <message>
                <location filename="src/shonDyMainWindow.ui" line="26"/>
                <source>Clicked Me</source>
                <translation></translation>
            </message>
            <message>
                <location filename="src/shonDyMainWindow.ui" line="43"/>
                <source>File</source>
                <translation type="unfinished">文件</translation>
            </message>
        </context>
        </TS>
        

        CMakeLists.txt

        cmake_minimum_required(VERSION 3.16.3)
        
        project(shonDyGUI LANGUAGES CXX C VERSION 2.6)
        
        
        ###############################
        # Setup project
        ###############################
        set(CMAKE_AUTOUIC ON)
        set(CMAKE_AUTOMOC ON)
        set(CMAKE_AUTORCC ON)
        
        set(CMAKE_CXX_STANDARD 17)
        set(CMAKE_CXX_STANDARD_REQUIRED ON)
        set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
        set(CMAKE_INSTALL_MESSAGE LAZY)
        set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DJSON_DIAGNOSTICS=1")
        
        find_package(VTK REQUIRED)
        find_package(Qt5 REQUIRED COMPONENTS Core Widgets LinguistTools Network Gui OpenGL Concurrent)
        find_package(Qt5Test REQUIRED)
        
        file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/*.cpp")
        file(GLOB_RECURSE UI_FILES "${PROJECT_SOURCE_DIR}/src/*.ui")
        file(GLOB_RECURSE HEADER_FILES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/*.h")
        
        set(TS_FILES
                ${PROJECT_SOURCE_DIR}/shonDy_zh_CN.ts
                )
        set_source_files_properties(${TS_FILES} PROPERTIES OUTPUT_LOCATION ${PROJECT_BINARY_DIR})
        qt5_create_translation(QM_FILES ${SRC_FILES} ${UI_FILES} ${HEADER_FILES} ${TS_FILES})
        
        add_executable(${PROJECT_NAME}
                ${SRC_FILES}
                ${UI_FILES}
                ${HEADER_FILES}
                ${QM_FILES}
                )
        
        
        target_link_libraries(${PROJECT_NAME}
                PRIVATE
                Qt5::Widgets
                )
        
        JonBJ 1 Reply Last reply
        0
        • A Alex Luya

          @Christian-Ehrlicher The reuslt is shown like this:
          Selection_032.png

          And all codes are listed as blow,including .ui and CMakeLists.txt

          main.cpp

          #include "shonDyMainWindow.h"
          #include <QApplication>
          
          int main(int argc, char** argv)
          {
              using namespace shonCloud;
          
              QApplication application(argc, argv);
          
              shonDyMainWindow mainWindow;
              mainWindow.show();
              return application.exec();
          }
          

          shonDyMainWindow.h

          #pragma once
          #include <QMainWindow>
          
          
          namespace Ui
          {
          class shonDyMainWindow;
          }
          namespace shonCloud
          {
          
          class shonDyMainWindow : public QMainWindow
          {
              Q_OBJECT
          
             public:
              explicit shonDyMainWindow(QWidget *parent = nullptr);
          
             private:
              Ui::shonDyMainWindow *ui;
          };
          
          }  // namespace shonCloud
          

          shonDyMainWindow.cpp

          #include "shonDyMainWindow.h"
          #include "ui_shonDyMainWindow.h"
          #include <QTranslator>
          #include <QPushButton>
          
          namespace shonCloud
          {
          shonDyMainWindow::shonDyMainWindow(QWidget *parent)
              : ui(new Ui::shonDyMainWindow)
          {
              ui->setupUi(this);
              auto pb=new QPushButton(tr("Add by tr"),ui->centralwidget);
              ui->verticalLayout->addWidget(pb);
              auto translator = new QTranslator;
              translator->load(R"(shonDy_zh_CN.qm)");
              QCoreApplication::instance()->installTranslator(translator);
              ui->pushButton->setText(tr("Hello World"));
          }
          
          }  // namespace shonCloud
          

          shonDyMainWindow.ui

          <?xml version="1.0" encoding="UTF-8"?>
          <ui version="4.0">
           <class>shonDyMainWindow</class>
           <widget class="QMainWindow" name="shonDyMainWindow">
            <property name="geometry">
             <rect>
              <x>0</x>
              <y>0</y>
              <width>1156</width>
              <height>765</height>
             </rect>
            </property>
            <property name="windowTitle">
             <string>MainWindow</string>
            </property>
            <widget class="QWidget" name="centralwidget">
             <layout class="QVBoxLayout" name="verticalLayout">
              <item>
               <widget class="QPushButton" name="pushButton">
                <property name="font">
                 <font>
                  <pointsize>19</pointsize>
                 </font>
                </property>
                <property name="text">
                 <string>Clicked Me</string>
                </property>
               </widget>
              </item>
             </layout>
            </widget>
            <widget class="QMenuBar" name="menubar">
             <property name="geometry">
              <rect>
               <x>0</x>
               <y>0</y>
               <width>1156</width>
               <height>18</height>
              </rect>
             </property>
             <widget class="QMenu" name="menuFile">
              <property name="title">
               <string>File</string>
              </property>
              <addaction name="separator"/>
             </widget>
             <addaction name="menuFile"/>
            </widget>
           </widget>
           <resources/>
           <connections/>
          </ui>
          

          shonDy_zh_CN.ts

          <?xml version="1.0" encoding="utf-8"?>
          <!DOCTYPE TS>
          <TS version="2.1" language="zh_CN">
          <context>
              <name>shonCloud::shonDyMainWindow</name>
              <message>
                  <location filename="src/shonDyMainWindow.cpp" line="14"/>
                  <source>Add by tr</source>
                  <translation type="unfinished">在CPP中添加</translation>
              </message>
              <message>
                  <location filename="src/shonDyMainWindow.cpp" line="19"/>
                  <source>Hello World</source>
                  <translation type="unfinished">你好,世界</translation>
              </message>
          </context>
          <context>
              <name>shonDyMainWindow</name>
              <message>
                  <location filename="src/shonDyMainWindow.ui" line="14"/>
                  <source>MainWindow</source>
                  <translation type="unfinished"></translation>
              </message>
              <message>
                  <location filename="src/shonDyMainWindow.ui" line="26"/>
                  <source>Clicked Me</source>
                  <translation></translation>
              </message>
              <message>
                  <location filename="src/shonDyMainWindow.ui" line="43"/>
                  <source>File</source>
                  <translation type="unfinished">文件</translation>
              </message>
          </context>
          </TS>
          

          CMakeLists.txt

          cmake_minimum_required(VERSION 3.16.3)
          
          project(shonDyGUI LANGUAGES CXX C VERSION 2.6)
          
          
          ###############################
          # Setup project
          ###############################
          set(CMAKE_AUTOUIC ON)
          set(CMAKE_AUTOMOC ON)
          set(CMAKE_AUTORCC ON)
          
          set(CMAKE_CXX_STANDARD 17)
          set(CMAKE_CXX_STANDARD_REQUIRED ON)
          set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
          set(CMAKE_INSTALL_MESSAGE LAZY)
          set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DJSON_DIAGNOSTICS=1")
          
          find_package(VTK REQUIRED)
          find_package(Qt5 REQUIRED COMPONENTS Core Widgets LinguistTools Network Gui OpenGL Concurrent)
          find_package(Qt5Test REQUIRED)
          
          file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/*.cpp")
          file(GLOB_RECURSE UI_FILES "${PROJECT_SOURCE_DIR}/src/*.ui")
          file(GLOB_RECURSE HEADER_FILES CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/src/*.h")
          
          set(TS_FILES
                  ${PROJECT_SOURCE_DIR}/shonDy_zh_CN.ts
                  )
          set_source_files_properties(${TS_FILES} PROPERTIES OUTPUT_LOCATION ${PROJECT_BINARY_DIR})
          qt5_create_translation(QM_FILES ${SRC_FILES} ${UI_FILES} ${HEADER_FILES} ${TS_FILES})
          
          add_executable(${PROJECT_NAME}
                  ${SRC_FILES}
                  ${UI_FILES}
                  ${HEADER_FILES}
                  ${QM_FILES}
                  )
          
          
          target_link_libraries(${PROJECT_NAME}
                  PRIVATE
                  Qt5::Widgets
                  )
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Alex-Luya
          Your title is "all tr() which aren't prefixed with QObject doesn't work". I don't see where any QObject is involved?

          You do tr("Add by tr") before you call translator->load(R"(shonDy_zh_CN.qm)"); while you do tr("Hello World") after calling it. I don't use translation, but I imagine tr() translates according to the loaded QTranslator in effect at the moment tr() is executed? If you change translator after any tr()s have been called I imagine you have to retranslate to force them to be re-evaluated?

          1 Reply Last reply
          3

          • Login

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