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. Change language at runtime
Forum Updated to NodeBB v4.3 + New Features

Change language at runtime

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 4.9k 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.
  • C Offline
    C Offline
    Cobra91151
    wrote on 9 Feb 2017, 12:41 last edited by
    #1

    Hi! I'm writing my application by code and I have implemented language change in runtime but I don't have ui.retranslateUi(this) function to change text. I have managed to change some text but problem is how to change combobox items text? Thanks in advance for your help.

    J 1 Reply Last reply 9 Feb 2017, 13:44
    0
    • C Cobra91151
      9 Feb 2017, 12:41

      Hi! I'm writing my application by code and I have implemented language change in runtime but I don't have ui.retranslateUi(this) function to change text. I have managed to change some text but problem is how to change combobox items text? Thanks in advance for your help.

      J Offline
      J Offline
      joeQ
      wrote on 9 Feb 2017, 13:44 last edited by
      #2

      @Cobra91151
      Hi, friend, welcome devnet.

      There is my way. I hope it can help you.

      1. First, Tow slots function when changed the language
      void MainWindow::StActionLanguageChina()
      {
          mpTranslator->load(":/lg/language/lg_ch.qm");// QTranslator load the language file
          qApp->installTranslator(mpTranslator);
      }
      
      void MainWindow::StActionLanguageEnglish()
      {
          m_pTranslator->load(":/lg/language/lg_en.qm");
          qApp->installTranslator(m_pTranslator);
      }
      
      1. rewrite the changeEvent function
      void MainWindow::changeEvent(QEvent *event)
      {
          //QDialog::changeEvent(event);
          if( QEvent::LanguageChange == event->type() )
          {
              ui->retranslateUi(this);
              ReUiSetText(); // note this function: if you hasn't the ui file, must use this way to translate
          }
      }
      
      1. If hasn't ui file, should use the way.
      void MainWindow::ReUiSetText()
      {
          m_pMenuFile->setTitle(tr("File(&F)"));
          m_pMenuSet->setTitle(tr("Set(&S)"));
          m_pMenuHelp->setTitle(tr("Help(&H)"));
          //...all the language what you want to translate, but not has the ui file. you should rewrite the `set....` at here. to change the language.
      }
      

      Just do it!

      C 1 Reply Last reply 9 Feb 2017, 13:54
      0
      • J joeQ
        9 Feb 2017, 13:44

        @Cobra91151
        Hi, friend, welcome devnet.

        There is my way. I hope it can help you.

        1. First, Tow slots function when changed the language
        void MainWindow::StActionLanguageChina()
        {
            mpTranslator->load(":/lg/language/lg_ch.qm");// QTranslator load the language file
            qApp->installTranslator(mpTranslator);
        }
        
        void MainWindow::StActionLanguageEnglish()
        {
            m_pTranslator->load(":/lg/language/lg_en.qm");
            qApp->installTranslator(m_pTranslator);
        }
        
        1. rewrite the changeEvent function
        void MainWindow::changeEvent(QEvent *event)
        {
            //QDialog::changeEvent(event);
            if( QEvent::LanguageChange == event->type() )
            {
                ui->retranslateUi(this);
                ReUiSetText(); // note this function: if you hasn't the ui file, must use this way to translate
            }
        }
        
        1. If hasn't ui file, should use the way.
        void MainWindow::ReUiSetText()
        {
            m_pMenuFile->setTitle(tr("File(&F)"));
            m_pMenuSet->setTitle(tr("Set(&S)"));
            m_pMenuHelp->setTitle(tr("Help(&H)"));
            //...all the language what you want to translate, but not has the ui file. you should rewrite the `set....` at here. to change the language.
        }
        
        C Offline
        C Offline
        Cobra91151
        wrote on 9 Feb 2017, 13:54 last edited by Cobra91151 2 Sept 2017, 13:57
        #3

        @joeQ
        Thanks but you misunderstood my question. I have already implemented my function Test::appLocalization() in which I change text, the problem is how to change QComboBox items text?

        QComboBox did not have property setTitle.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 9 Feb 2017, 13:59 last edited by
          #4

          You basically have to re-execute any line of code that calls tr() so in case of QComboBox:

          // QComboBox* comboBox
          auto comboModel = comboBox.model();
          comboModel->setData(comboModel->index(0,0),tr("Option1"));
          comboModel->setData(comboModel->index(1,0),tr("Option2"));
          comboModel->setData(comboModel->index(2,0),tr("Option3"));
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          K C 2 Replies Last reply 9 Feb 2017, 14:08
          1
          • V VRonin
            9 Feb 2017, 13:59

            You basically have to re-execute any line of code that calls tr() so in case of QComboBox:

            // QComboBox* comboBox
            auto comboModel = comboBox.model();
            comboModel->setData(comboModel->index(0,0),tr("Option1"));
            comboModel->setData(comboModel->index(1,0),tr("Option2"));
            comboModel->setData(comboModel->index(2,0),tr("Option3"));
            
            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 9 Feb 2017, 14:08 last edited by
            #5

            That's one thing the designer shines at - retranslating the whole of the static UI automagically ... :)

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            1
            • V VRonin
              9 Feb 2017, 13:59

              You basically have to re-execute any line of code that calls tr() so in case of QComboBox:

              // QComboBox* comboBox
              auto comboModel = comboBox.model();
              comboModel->setData(comboModel->index(0,0),tr("Option1"));
              comboModel->setData(comboModel->index(1,0),tr("Option2"));
              comboModel->setData(comboModel->index(2,0),tr("Option3"));
              
              C Offline
              C Offline
              Cobra91151
              wrote on 9 Feb 2017, 14:15 last edited by
              #6

              @VRonin

              I have changed your code to:
              comboBox->model()->setData(comboBox->model()->index(0,0), QObject::tr("Text"));

              And now it's retranslating QComboBox. I post it here so others can find the solution. Thank you for your reply.

              1 Reply Last reply
              1
              • JonBJ JonB referenced this topic on 10 Mar 2023, 09:14

              1/6

              9 Feb 2017, 12:41

              • Login

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