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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on 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.

    joeQJ 1 Reply Last reply
    0
    • Cobra91151C Cobra91151

      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.

      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on 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!

      Cobra91151C 1 Reply Last reply
      0
      • joeQJ joeQ

        @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.
        }
        
        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by Cobra91151
        #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
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on 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

          kshegunovK Cobra91151C 2 Replies Last reply
          1
          • VRoninV VRonin

            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"));
            
            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on 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
            • VRoninV VRonin

              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"));
              
              Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on 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

              • Login

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