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. Translate texts in QTableWidget

Translate texts in QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 1.4k Views 3 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.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by
    #1

    I started using Qt Linguist yesterday and I was able to successfully translate my application to the desired language but only the texts in widgets.

    My application has a QTableWidget with headers and texts to be added dynamically in the rows that need to be translated but they aren't translated together with the widgets' texts.

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ui->tableWidget->setColumnCount(3);
    
        QStringList headers;
        headers << tr("Column1") << tr("Column2") << tr("Column3") ;
    
        ui->tableWidget->setHorizontalHeaderLabels(headers);
    
        if(translator.load(":/Languages/Languages/translation.qm"))
                qDebug() << "OK";
    }
    
    

    I thought that by using the function tr() in QString literals would suffice to make it translatable.

    Also I have a function that adds rows to the table and they have texts to be translated too.

    What am I am missing?

    artwawA Pablo J. RoginaP 2 Replies Last reply
    0
    • H hbatalha

      I started using Qt Linguist yesterday and I was able to successfully translate my application to the desired language but only the texts in widgets.

      My application has a QTableWidget with headers and texts to be added dynamically in the rows that need to be translated but they aren't translated together with the widgets' texts.

      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          ui->tableWidget->setColumnCount(3);
      
          QStringList headers;
          headers << tr("Column1") << tr("Column2") << tr("Column3") ;
      
          ui->tableWidget->setHorizontalHeaderLabels(headers);
      
          if(translator.load(":/Languages/Languages/translation.qm"))
                  qDebug() << "OK";
      }
      
      

      I thought that by using the function tr() in QString literals would suffice to make it translatable.

      Also I have a function that adds rows to the table and they have texts to be translated too.

      What am I am missing?

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      @hbatalha Hi,
      how is Linguist supposed to know anything about the data that is dynamically added to the widget during the runtime?

      Usual approach would be to have different (translated or otherwise locale aware) data per language and load them depending on the selected language. But you need to take care of that behaviour yourself.

      For more information please re-read.

      Kind Regards,
      Artur

      H 1 Reply Last reply
      3
      • artwawA artwaw

        @hbatalha Hi,
        how is Linguist supposed to know anything about the data that is dynamically added to the widget during the runtime?

        Usual approach would be to have different (translated or otherwise locale aware) data per language and load them depending on the selected language. But you need to take care of that behaviour yourself.

        H Offline
        H Offline
        hbatalha
        wrote on last edited by
        #3

        @artwaw

        how is Linguist supposed to know anything about the data that is dynamically added to the widget during the runtime?

        But it is just text, a QString declared in the source code.

        Usual approach would be to have different (translated or otherwise locale aware) data per language and load them depending on the selected language.

        Any reference?

        artwawA 1 Reply Last reply
        0
        • H hbatalha

          @artwaw

          how is Linguist supposed to know anything about the data that is dynamically added to the widget during the runtime?

          But it is just text, a QString declared in the source code.

          Usual approach would be to have different (translated or otherwise locale aware) data per language and load them depending on the selected language.

          Any reference?

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @hbatalha If that text is in the code and, as you say, included in tr(), then I misunderstood. Sorry.
          Can you show that code please?
          Also, please make sure that your translations adhere to the notation the translator expects.

          For more information please re-read.

          Kind Regards,
          Artur

          H 1 Reply Last reply
          0
          • H hbatalha

            I started using Qt Linguist yesterday and I was able to successfully translate my application to the desired language but only the texts in widgets.

            My application has a QTableWidget with headers and texts to be added dynamically in the rows that need to be translated but they aren't translated together with the widgets' texts.

            MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)
                , ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            
                ui->tableWidget->setColumnCount(3);
            
                QStringList headers;
                headers << tr("Column1") << tr("Column2") << tr("Column3") ;
            
                ui->tableWidget->setHorizontalHeaderLabels(headers);
            
                if(translator.load(":/Languages/Languages/translation.qm"))
                        qDebug() << "OK";
            }
            
            

            I thought that by using the function tr() in QString literals would suffice to make it translatable.

            Also I have a function that adds rows to the table and they have texts to be translated too.

            What am I am missing?

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @hbatalha said in Translate texts in QTableWidget:

            QStringList headers;
            headers << tr("Column1") << tr("Column2") << tr("Column3") ;

            ui->tableWidget->setHorizontalHeaderLabels(headers);
            
            if(translator.load(":/Languages/Languages/translation.qm"))
                    qDebug() << "OK";
            

            You're installing the translator AFTER you ask for the translation of headers (the tr() calls)

            BTW, the translator(s) is(are) installed at QApp level so when you're working with any widgets (i.e. MainWindow) the translated strings are available

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            H 1 Reply Last reply
            3
            • artwawA artwaw

              @hbatalha If that text is in the code and, as you say, included in tr(), then I misunderstood. Sorry.
              Can you show that code please?
              Also, please make sure that your translations adhere to the notation the translator expects.

              H Offline
              H Offline
              hbatalha
              wrote on last edited by
              #6

              @artwaw said in Translate texts in QTableWidget:

              Can you show that code please?

              It is something like this:

              void MainWindow::loadSettings()
              {
                  QSettings qsettings(QSettings::IniFormat, QSettings::UserScope, "HBatalha", "Table");
              
                 int size = qsettings.beginReadArray("Table");
                 
                 for(int i = 0; i < size; ++i)    
                 {        
                      QStringList args = qsettings.value("args").toString().split(" ");
                      
                      FooClass::Status status = static_cast<FooClass::Status>(qsettings.value("status").toInt());        
                     const int dest_row = ui->tableWidget->rowCount();
              
                      ui->tableWidget->setRowCount(dest_row + 1);
              
                      QString status_str;
              
                      switch (status)
                      {
                      case Downloader::Status::DONE:
                          status_str= tr("Done"); // Here
                          break;
              
                      case Downloader::Status::PAUSED:
                          status_str= tr("Paused"); // and here
                          break;
                       }
              
                      ui->tableWidget->setItem(dest_row, 0, new QTableWidgetItem(status_str);
                      ui->tableWidget->setItem(dest_row, 1, new QTableWidgetItem("Text2"));
              
                  }
                  qsettings.endArray();
              }
              
              1 Reply Last reply
              0
              • Pablo J. RoginaP Pablo J. Rogina

                @hbatalha said in Translate texts in QTableWidget:

                QStringList headers;
                headers << tr("Column1") << tr("Column2") << tr("Column3") ;

                ui->tableWidget->setHorizontalHeaderLabels(headers);
                
                if(translator.load(":/Languages/Languages/translation.qm"))
                        qDebug() << "OK";
                

                You're installing the translator AFTER you ask for the translation of headers (the tr() calls)

                BTW, the translator(s) is(are) installed at QApp level so when you're working with any widgets (i.e. MainWindow) the translated strings are available

                H Offline
                H Offline
                hbatalha
                wrote on last edited by
                #7

                @Pablo-J-Rogina said in Translate texts in QTableWidget:

                You're installing the translator AFTER you ask for the translation of headers (the tr() calls)

                I changed that and it it is still the same.

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  hbatalha
                  wrote on last edited by
                  #8

                  A little update: I was able to translate the headers by disabling the code ui->tableWidget->setHorizontalHeaderLabels(headers); in the constructor and add the headers in the Design.

                  mrjjM 1 Reply Last reply
                  0
                  • H hbatalha

                    A little update: I was able to translate the headers by disabling the code ui->tableWidget->setHorizontalHeaderLabels(headers); in the constructor and add the headers in the Design.

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

                    @hbatalha
                    Hi
                    tr() does make it translatable.
                    BUT
                    all text is not by reference so
                    if you give some text to a widget and then load a new translator the
                    text in the widget is not affected as its a copy.

                    so to use the new text, you must set the text again.

                    the reason it works when done via design is that code is generated to reapply it

                     void retranslateUi(QMainWindow *MainWindow)
                        {
                           QTableWidgetItem *___qtablewidgetitem = tableWidget->horizontalHeaderItem(0);        
                        ___qtablewidgetitem->setText(QCoreApplication::translate("MainWindow", "New Column", nullptr));
                    
                    H 1 Reply Last reply
                    3
                    • mrjjM mrjj

                      @hbatalha
                      Hi
                      tr() does make it translatable.
                      BUT
                      all text is not by reference so
                      if you give some text to a widget and then load a new translator the
                      text in the widget is not affected as its a copy.

                      so to use the new text, you must set the text again.

                      the reason it works when done via design is that code is generated to reapply it

                       void retranslateUi(QMainWindow *MainWindow)
                          {
                             QTableWidgetItem *___qtablewidgetitem = tableWidget->horizontalHeaderItem(0);        
                          ___qtablewidgetitem->setText(QCoreApplication::translate("MainWindow", "New Column", nullptr));
                      
                      H Offline
                      H Offline
                      hbatalha
                      wrote on last edited by
                      #10

                      @mrjj
                      Thank you very much for your insight, this solved my problem.

                      I created a function that retranslates the table.

                      1 Reply Last reply
                      1

                      • Login

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