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. Translation and QStringList
Forum Updated to NodeBB v4.3 + New Features

Translation and QStringList

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 801 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.
  • S Offline
    S Offline
    Sevi
    wrote on last edited by
    #1

    Helllo

    I am currently trying my hand at a small program with QT and have now encountered the following problem:
    I have created a translation file and specified what I want to translate in the code using "tr("")". In QT Linguist I can also translate these entries successfully.
    Loading the translation also works perfectly.
    However, I have a QStringList to fill a QTableWidget with the header information. There I have also specified the "tr("")", which is also displayed in QLinguist.
    However, no translation is displayed in the window even after recompiling.
    To rule out a direct error, I simply translated a QLabel and set it directly in the code like this: ui.qLabel->setText(tr("Hello"));
    I then "translated" this in QLinguist using lrelease and created the *qm file again and tried it out. This translation is displayed to me.
    I have the following as QStringList:

        QStringList headerLabels;
         
        	headerLabels << tr("Order ID") << tr("Order Number") << tr("Line or machine") << tr("Order priority") << tr("Storage location") << tr("Article name") << tr("Article number") << tr("Article Quantity");
        	headerLabels << tr("Ordered first name") << tr("Orderer last name") << tr("More information") << tr("Cost unit") << tr("Order date");
    

    and set it with

    ui.QTableWidghet->setHorizontalHeaderLabels(headerLabels);
    

    What else could be the reason that the translation is not displayed like this?
    Many thanks for the answers.

    I Use QT 6.6.1

    Sevi

    JonBJ 1 Reply Last reply
    0
    • S Sevi

      Helllo

      I am currently trying my hand at a small program with QT and have now encountered the following problem:
      I have created a translation file and specified what I want to translate in the code using "tr("")". In QT Linguist I can also translate these entries successfully.
      Loading the translation also works perfectly.
      However, I have a QStringList to fill a QTableWidget with the header information. There I have also specified the "tr("")", which is also displayed in QLinguist.
      However, no translation is displayed in the window even after recompiling.
      To rule out a direct error, I simply translated a QLabel and set it directly in the code like this: ui.qLabel->setText(tr("Hello"));
      I then "translated" this in QLinguist using lrelease and created the *qm file again and tried it out. This translation is displayed to me.
      I have the following as QStringList:

          QStringList headerLabels;
           
          	headerLabels << tr("Order ID") << tr("Order Number") << tr("Line or machine") << tr("Order priority") << tr("Storage location") << tr("Article name") << tr("Article number") << tr("Article Quantity");
          	headerLabels << tr("Ordered first name") << tr("Orderer last name") << tr("More information") << tr("Cost unit") << tr("Order date");
      

      and set it with

      ui.QTableWidghet->setHorizontalHeaderLabels(headerLabels);
      

      What else could be the reason that the translation is not displayed like this?
      Many thanks for the answers.

      I Use QT 6.6.1

      Sevi

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Sevi
      I know nothing about "Linguist" or ".qm" files. But if you are suggesting that in, say,

      QString s = tr("...");
      QStringList sl << tr("...");
      

      the tr() somehow works in the first statement but not in the QStringList statement that is simply not possible.

      Start with a qDebug() << headerLabels to confirm what is in there has been translated before you do anything with it.

      Christian EhrlicherC S 2 Replies Last reply
      0
      • JonBJ JonB

        @Sevi
        I know nothing about "Linguist" or ".qm" files. But if you are suggesting that in, say,

        QString s = tr("...");
        QStringList sl << tr("...");
        

        the tr() somehow works in the first statement but not in the QStringList statement that is simply not possible.

        Start with a qDebug() << headerLabels to confirm what is in there has been translated before you do anything with it.

        Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        ... and then check that you don't override your header labels later on somewhere else.

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

        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          ... and then check that you don't override your header labels later on somewhere else.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Christian-Ehrlicher ...which is the most likely situation, if one had to guess, assuming the OP sees them revert to something...

          1 Reply Last reply
          0
          • JonBJ JonB

            @Sevi
            I know nothing about "Linguist" or ".qm" files. But if you are suggesting that in, say,

            QString s = tr("...");
            QStringList sl << tr("...");
            

            the tr() somehow works in the first statement but not in the QStringList statement that is simply not possible.

            Start with a qDebug() << headerLabels to confirm what is in there has been translated before you do anything with it.

            S Offline
            S Offline
            Sevi
            wrote on last edited by
            #5

            I think I have found the main problem. I called the translation file once in the class header and thought that would be enough, apparently not. I now call it in the main function and the QStringList is now translated and displayed correctly.
            However, the error seems to persist somehow.
            I have the following in it now:

            ui.lb_openCourierOrders->setText(tr("Hello Test"));
               qDebug() << tr("Hello Test");
            

            I get this as the output of qDebug():

            "This is a bigger test or something"
            

            so it seems to be translated, but this change is not displayed.
            Could it be that I have chosen the wrong setting for the label or something?

            JonBJ 1 Reply Last reply
            0
            • S Sevi

              I think I have found the main problem. I called the translation file once in the class header and thought that would be enough, apparently not. I now call it in the main function and the QStringList is now translated and displayed correctly.
              However, the error seems to persist somehow.
              I have the following in it now:

              ui.lb_openCourierOrders->setText(tr("Hello Test"));
                 qDebug() << tr("Hello Test");
              

              I get this as the output of qDebug():

              "This is a bigger test or something"
              

              so it seems to be translated, but this change is not displayed.
              Could it be that I have chosen the wrong setting for the label or something?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Sevi
              No that is not possible. tr() is not magic, it's just a function call which takes a string and returns a new, translated string. And if you call label->setText(tr(....)) then it will set the label's text to the translated string. Again suggest you look at your code carefully, sounds like you do stuff after a translated string has been set which changes e.g. what is in the label.

              S 1 Reply Last reply
              0
              • JonBJ JonB

                @Sevi
                No that is not possible. tr() is not magic, it's just a function call which takes a string and returns a new, translated string. And if you call label->setText(tr(....)) then it will set the label's text to the translated string. Again suggest you look at your code carefully, sounds like you do stuff after a translated string has been set which changes e.g. what is in the label.

                S Offline
                S Offline
                Sevi
                wrote on last edited by
                #7

                @JonB
                I realize that. Hence the question of whether you could possibly "adjust" something in the QWidget or QLabel settings so that it no longer works.
                I created a new window and used a label there and everything works there...so the problem with the other window must be with something else that I still have to find.

                Thanks for the quick answers though.

                JonBJ 1 Reply Last reply
                0
                • S Sevi

                  @JonB
                  I realize that. Hence the question of whether you could possibly "adjust" something in the QWidget or QLabel settings so that it no longer works.
                  I created a new window and used a label there and everything works there...so the problem with the other window must be with something else that I still have to find.

                  Thanks for the quick answers though.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Sevi said in Translation and QStringList:

                  whether you could possibly "adjust" something in the QWidget or QLabel settings so that it no longer works.

                  No, or else I would have said so. And they don't know about translating anyway, that's the point of passing a tr()ed string, that is where the translation takes place. You are then unconditionally setting its text to the string passed.

                  Again, assuming the tr()ed string you are passing is correct, either you are calling ui.lb_openCourierOrders->setText() again later or you are recreating the original widget or something.

                  Unfortunately, changing the text of a QLabel does not emit any signal, which could have helped spot where else this might be happening from. You'll have to look through your code, or use a debugger. Start by searching all files for lb_openCourierOrders->setText(!

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

                    Hi,

                    Where and when exactly are you setting up your translator ?

                    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
                    0

                    • Login

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