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. QDialog content not being translated
Qt 6.11 is out! See what's new in the release blog

QDialog content not being translated

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 4 Posters 5.8k 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.
  • J Jayant

    Hi @J-Hilk I am not using default Qt Dialogs. I am using custom dialogs with extend the QDialog class and having a ui file.

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

    @Jayant you may want to override the languageChange() method in your custom dialog class. Something like this (pseudo-code):

    /**
     * Handles the event about language changed.
     * Retranslate all applicable strings.
     *
     */
    void
    YourDialog::languageChange()
    {
        ui->retranslateUi(this);
        this->setWindowTitle(tr(Confirm Action");
        // Change label of default Ok button to "Remove"
        ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Remove"));
    }
    

    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

    1 Reply Last reply
    3
    • J Offline
      J Offline
      Jayant
      wrote on last edited by
      #5

      Hi @Pablo-J-Rogina thanks for your reply, tried this but nothing happens.

      JonBJ 1 Reply Last reply
      0
      • J Jayant

        Hi @Pablo-J-Rogina thanks for your reply, tried this but nothing happens.

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

        @Jayant
        Sorry to butt in, but: are you using tr("...") like @Pablo-J-Rogina shows? In which case, I would have thought dialogs/menus/whatever is irrelevant, tr() is tr() and either works or does not regardless of where it happens to be called from?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jayant
          wrote on last edited by
          #7

          @JonB
          Yes ofcourse here's my code

          void Dialog::changeEvent(QEvent *e){
              if (e->type() == QEvent::LanguageChange) {
                  ui->retranslateUi(this);
                  setWindowTitle(tr("Test Modal Dialog"));
                  ui->label->setText(tr("this is a label"));
              } else {
                  QWidget::changeEvent(e);
              }
          }
          
          JonBJ 1 Reply Last reply
          0
          • J Jayant

            @JonB
            Yes ofcourse here's my code

            void Dialog::changeEvent(QEvent *e){
                if (e->type() == QEvent::LanguageChange) {
                    ui->retranslateUi(this);
                    setWindowTitle(tr("Test Modal Dialog"));
                    ui->label->setText(tr("this is a label"));
                } else {
                    QWidget::changeEvent(e);
                }
            }
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #8

            @Jayant
            Well, start by verifying (that this code is hit and) what the tr("...")s here are actually returning, rather than setting text from them.

            Also, I don't know, but after your code in the LanguageChange case, do you need to still call the base QWidget::changeEvent(e); to let it process? (EDIT OK, I see the examples in the docs.) And why are you calling QWidget::changeEvent() instead of QDialog::changeEvent()?

            1 Reply Last reply
            1
            • J Offline
              J Offline
              Jayant
              wrote on last edited by
              #9

              @JonB
              Change QWidget::changeEvent(e) to QDialog::changeEvent(e) but nothing happens

              JonBJ 1 Reply Last reply
              0
              • J Jayant

                @JonB
                Change QWidget::changeEvent(e) to QDialog::changeEvent(e) but nothing happens

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

                @Jayant
                What are the tr("...")s returning?
                And please just confirm your method is being hit, for all we know it might not be?

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jayant
                  wrote on last edited by
                  #11

                  @JonB
                  You are right this function is not being hit.

                  JonBJ J.HilkJ 2 Replies Last reply
                  0
                  • J Jayant

                    @JonB
                    You are right this function is not being hit.

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

                    @Jayant
                    Which is why I asked you that twice! Suggest always verify a function is being hit if you put code into it! :)

                    1 Reply Last reply
                    0
                    • J Jayant

                      @JonB
                      You are right this function is not being hit.

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

                      @Jayant that's obvious, as the Dialogs are created on the fly during run.

                      But if the translations exist, and the translation is loaded correctly, then the new created widget/dialog will also show the appropriate translated text.

                      You should show some more of your code, and double check that the translation really exists and is the correct one (assigned to the correct source code)


                      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.

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

                        @Jayant that's obvious, as the Dialogs are created on the fly during run.

                        But if the translations exist, and the translation is loaded correctly, then the new created widget/dialog will also show the appropriate translated text.

                        You should show some more of your code, and double check that the translation really exists and is the correct one (assigned to the correct source code)

                        J Offline
                        J Offline
                        Jayant
                        wrote on last edited by
                        #14

                        @J-Hilk
                        Translation is loaded correctly because the mainwindow shows up the translated text but the dialog class doesn't. Following code translates the text

                        QCheckBox *checkBox = qobject_cast<QCheckBox *>(sender());
                            MainWindow *window = mainWindowForCheckBoxMap[checkBox];
                            if (!window) {
                                QTranslator translator;
                                translator.load(qmFileForCheckBoxMap[checkBox]);
                                qApp->installTranslator(&translator);
                        
                                window = new MainWindow;
                                window->setPalette(colorForLanguage(checkBox->text()));
                        
                                window->installEventFilter(this);
                                mainWindowForCheckBoxMap.insert(checkBox, window);
                            }
                            window->setVisible(checkBox->isChecked());
                        
                        J.HilkJ 1 Reply Last reply
                        0
                        • J Jayant

                          @J-Hilk
                          Translation is loaded correctly because the mainwindow shows up the translated text but the dialog class doesn't. Following code translates the text

                          QCheckBox *checkBox = qobject_cast<QCheckBox *>(sender());
                              MainWindow *window = mainWindowForCheckBoxMap[checkBox];
                              if (!window) {
                                  QTranslator translator;
                                  translator.load(qmFileForCheckBoxMap[checkBox]);
                                  qApp->installTranslator(&translator);
                          
                                  window = new MainWindow;
                                  window->setPalette(colorForLanguage(checkBox->text()));
                          
                                  window->installEventFilter(this);
                                  mainWindowForCheckBoxMap.insert(checkBox, window);
                              }
                              window->setVisible(checkBox->isChecked());
                          
                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #15

                          @Jayant
                          not quite what I had in mind,
                          Your dialogs, and how they get their text

                          Translation is loaded correctly because the mainwindow shows up the translated text but the dialog class doesn't. Following code translates the text

                          I was rather asking if you checked that the translatable strings for your dialog where in your *qm file and correctly assigned.


                          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.

                          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