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. Trying to insert tr() returned string in Qhash
Qt 6.11 is out! See what's new in the release blog

Trying to insert tr() returned string in Qhash

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 788 Views
  • 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
    Ankit1999
    wrote on last edited by
    #1

    I am trying to createv a QHash <QString, QString> type and inserting the following key values

    const QHash <QString, QString> ClassName:MyHash = 
    {
     {("test"), tr("Test")},
    {("test1"), tr("Test1")}
    }
    

    when launching the application its crashing when executing the above code.

    jsulmJ Christian EhrlicherC 2 Replies Last reply
    0
    • A Ankit1999

      I am trying to createv a QHash <QString, QString> type and inserting the following key values

      const QHash <QString, QString> ClassName:MyHash = 
      {
       {("test"), tr("Test")},
      {("test1"), tr("Test1")}
      }
      

      when launching the application its crashing when executing the above code.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Ankit1999 said in Trying to insert tr() returned string in Qhash:

      application its crashing

      How does stack trace look like?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Ankit1999
        wrote on last edited by Ankit1999
        #3

        @jsulm i am getting an access violation reading location .. in the call stack i see that it has entered the tr() method and crashed internally in some qt core dll

        jsulmJ J.HilkJ 2 Replies Last reply
        0
        • A Ankit1999

          @jsulm i am getting an access violation reading location .. in the call stack i see that it has entered the tr() method and crashed internally in some qt core dll

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Ankit1999 Please post the stack trace

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Ankit1999

            @jsulm i am getting an access violation reading location .. in the call stack i see that it has entered the tr() method and crashed internally in some qt core dll

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #5

            @Ankit1999 I think this is going to be a problem.

            try const char * instead of a tr marked string:

            const QHash <QString, const char *> ClassName:MyHash
            

            than mark the string literal for translation:

            const QHash <QString, const char *> ClassName:MyHash = 
            {
                {("test"), QT_TRANSLATE_NOOP("MyHash", "Test")},
                {("test1"), QT_TRANSLATE_NOOP("MyHash", "Test1")}
            }
            

            than send the string literal through the translation unit on read:

            QCoreApplication::translate("MyHash", myHash.value("test"));
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            A 1 Reply Last reply
            1
            • A Ankit1999

              I am trying to createv a QHash <QString, QString> type and inserting the following key values

              const QHash <QString, QString> ClassName:MyHash = 
              {
               {("test"), tr("Test")},
              {("test1"), tr("Test1")}
              }
              

              when launching the application its crashing when executing the above code.

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

              @Ankit1999 said in Trying to insert tr() returned string in Qhash:

              ClassName:MyHash

              This does not even compile so how should it crash?

              Please post either the stack trace or a minimal, compilable example.

              I would guess it's a global static which gets initialized before Q(Core)Application so tr() will throw an assert. Don't use global statics (esp. not for such stuff where it's obviouslynot needed at all)

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

              1 Reply Last reply
              1
              • J.HilkJ J.Hilk

                @Ankit1999 I think this is going to be a problem.

                try const char * instead of a tr marked string:

                const QHash <QString, const char *> ClassName:MyHash
                

                than mark the string literal for translation:

                const QHash <QString, const char *> ClassName:MyHash = 
                {
                    {("test"), QT_TRANSLATE_NOOP("MyHash", "Test")},
                    {("test1"), QT_TRANSLATE_NOOP("MyHash", "Test1")}
                }
                

                than send the string literal through the translation unit on read:

                QCoreApplication::translate("MyHash", myHash.value("test"));
                
                A Offline
                A Offline
                Ankit1999
                wrote on last edited by
                #7

                @J-Hilk how do i add the translated strings using QT_TRANSLATE_NOOP the translated strings are not present in the hash even when i add the strings in qt linguist.

                J.HilkJ 1 Reply Last reply
                0
                • A Ankit1999

                  @J-Hilk how do i add the translated strings using QT_TRANSLATE_NOOP the translated strings are not present in the hash even when i add the strings in qt linguist.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @Ankit1999 said in Trying to insert tr() returned string in Qhash:

                  @J-Hilk how do i add the translated strings using QT_TRANSLATE_NOOP the translated strings are not present in the hash even when i add the strings in qt linguist.

                  ?
                  It is near literally a working example. let me make it one:

                  int main(int argc, char *argv[])
                  {
                  
                      QApplication app(argc,argv);
                  
                      const QHash <QString, const char *> myHash = 
                      {
                          {("test"), QT_TRANSLATE_NOOP("MyHash", "Test")},
                          {("test1"), QT_TRANSLATE_NOOP("MyHash", "Test1")}
                      };
                      
                  
                      qDebug() << QCoreApplication::translate("MyHash", myHash.value("test"));
                      qDebug() << QCoreApplication::translate("MyHash", myHash.value("test1"));
                      
                      QTranslator t;
                      qDebug() << "Loading translation" << t.load("bla.ts");
                      app.installTranslator(&t);
                      
                      qDebug() << QCoreApplication::translate("MyHash", myHash.value("test"));
                      qDebug() << QCoreApplication::translate("MyHash", myHash.value("test1"));
                  
                      return app.exec();
                  }
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  A 1 Reply Last reply
                  4
                  • J.HilkJ J.Hilk

                    @Ankit1999 said in Trying to insert tr() returned string in Qhash:

                    @J-Hilk how do i add the translated strings using QT_TRANSLATE_NOOP the translated strings are not present in the hash even when i add the strings in qt linguist.

                    ?
                    It is near literally a working example. let me make it one:

                    int main(int argc, char *argv[])
                    {
                    
                        QApplication app(argc,argv);
                    
                        const QHash <QString, const char *> myHash = 
                        {
                            {("test"), QT_TRANSLATE_NOOP("MyHash", "Test")},
                            {("test1"), QT_TRANSLATE_NOOP("MyHash", "Test1")}
                        };
                        
                    
                        qDebug() << QCoreApplication::translate("MyHash", myHash.value("test"));
                        qDebug() << QCoreApplication::translate("MyHash", myHash.value("test1"));
                        
                        QTranslator t;
                        qDebug() << "Loading translation" << t.load("bla.ts");
                        app.installTranslator(&t);
                        
                        qDebug() << QCoreApplication::translate("MyHash", myHash.value("test"));
                        qDebug() << QCoreApplication::translate("MyHash", myHash.value("test1"));
                    
                        return app.exec();
                    }
                    
                    A Offline
                    A Offline
                    Ankit1999
                    wrote on last edited by
                    #9

                    @J-Hilk thank you its working

                    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