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. [solved] Translation on the fly crashes
QtWS25 Last Chance

[solved] Translation on the fly crashes

Scheduled Pinned Locked Moved General and Desktop
32 Posts 2 Posters 10.3k 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.
  • S Offline
    S Offline
    skwateur
    wrote on last edited by
    #1

    Hello everyone.

    I am developping an application in French and need to translate it into English aswell.
    It was working fine till yesterday when I had a new idea about the way to change the language. At first I was using a menu bar to translate the application, then I wanted to do it through a new widget.

    The former case worked perfectly but the latter one failed so I decided to remove it. The problem is... translation through the menu bar doesn't work anymore now.

    I have a MainWindow composed of a mainWidget and this mainwidget is used to display "other" widgets by using buttons.
    Anyway, here is how I implemented the translation :

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    translator = new QTranslator(0);
    QApplication::instance()->installTranslator(translator);

    connect(item1, SIGNAL(triggered()), this, (SLOT(changeToFrench())));
    connect(item2, SIGNAL(triggered()), this, (SLOT(changeToEnglish())));@
    

    @void MainWindow::changeToFrench()
    {
    if(!translator->isEmpty())
    QApplication::instance()->removeTranslator(translator);
    }

    void MainWindow::changeToEnglish()
    {
    translator->load("dcl_en",QApplication::applicationDirPath());
    }@

    @void MainWindow::changeEvent(QEvent *e)
    {
    if(e->type() == QEvent::LanguageChange)
    {
    ui->retranslateUi(this);
    retranslateUi();
    }
    QMainWindow::changeEvent(e);
    }

    void MainWindow::retranslateUi()
    {
    menu1->setTitle(tr("Langue"));
    menu2->setTitle(tr("A propos..."));
    item4->setText(tr("Paramètres système"));
    item1->setText("Français");
    item2->setText("English");
    item3->setText(tr("A propos..."));
    }@

    The translation works fine when I am in the main menu but if I wanna change the language when I am within an "other widget" (a widget called ui_identity for instance) it crashes with exit code -1073741819.

    Here is the code in "ui_identity" :

    @void ui_identity::changeEvent(QEvent *e)
    {
    if(e->type() == QEvent::LanguageChange)
    {
    ui->retranslateUi(this);
    }
    }@

    Any ideas ?

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

      Hi,

      What does a run trough the debugger tells you ?

      Are you sure all the pointers your are using are valid ?

      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
      • S Offline
        S Offline
        skwateur
        wrote on last edited by
        #3

        Debugger tells me segmentation fault.
        The error is related to the translation

        1 Reply Last reply
        0
        • S Offline
          S Offline
          skwateur
          wrote on last edited by
          #4

          I can change the language through a setting window when I'm on the MainWindow but if I try while I'm on ui_identity wdget or some other widget it crashes...

          Really have no idea why.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            skwateur
            wrote on last edited by
            #5

            @qApp->installTranslator(translator);@ this line causes the segmentation fault apparently..

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

              Are you sure translator is valid ?

              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
              • S Offline
                S Offline
                skwateur
                wrote on last edited by
                #7

                @MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
                {
                translator = new QTranslator(0);@

                I can translate the whole program while being on MainWindow. As soon as I go into a widget, I can no longer translate the program. It crashes :s

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

                  That's in the MainWindow, how do you do it in your widget ?

                  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
                  • S Offline
                    S Offline
                    skwateur
                    wrote on last edited by
                    #9

                    I emit a signal from the widget which is then caught by the main window :
                    WIDGET :
                    @void ui_sys_settings::languageToEn()
                    {
                    emit toEnglish();
                    }

                    void ui_sys_settings::languageToFr()
                    {
                    emit toFrench();
                    }@

                    MAINWINDOW :
                    @connect(sysSettings,SIGNAL(toEnglish()),this,SLOT(changeToEnglish()));
                    connect(sysSettings,SIGNAL(toFrench()),this,SLOT(changeToFrench()));

                    void MainWindow::changeToFrench()
                    {
                    if(!translator->isEmpty())
                    qApp->removeTranslator(translator);
                    }

                    void MainWindow::changeToEnglish()
                    {
                    if(!translator->isEmpty())
                    qApp->removeTranslator(translator);

                    translator->load("dcl_en",QApplication::applicationDirPath());
                    qApp->installTranslator(translator);
                    

                    }@

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

                      Are you sure you need to re-install the 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
                      • S Offline
                        S Offline
                        skwateur
                        wrote on last edited by
                        #11

                        I only install it when I load the english language file. It is never installed otherwise

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

                          Strange...
                          Could you post/share a minimal compilable example that shows the problem ?

                          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
                          • S Offline
                            S Offline
                            skwateur
                            wrote on last edited by
                            #13

                            hmmm. post an example from my own program ?

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

                              Rather the minimal code that reproduce the crash

                              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
                              • S Offline
                                S Offline
                                skwateur
                                wrote on last edited by
                                #15

                                Oh. The code that produce the crash is the code I have posted above.

                                If I take the instruction " qApp->installTranslator(translator);" off, then it doesn't crash. But I have nothing translated lol

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

                                  Indeed, but with only pieces of code, I can't try to reproduce the crash.

                                  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
                                  • S Offline
                                    S Offline
                                    skwateur
                                    wrote on last edited by
                                    #17

                                    oh. Do you want the full code of the class ?

                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      skwateur
                                      wrote on last edited by
                                      #18

                                      Or I can send you the source files by email if you want. Maybe it could be easier for you to see what's wrong

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

                                        You can also use a service like "pastbin":http://pastebin.com/

                                        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
                                        • S Offline
                                          S Offline
                                          skwateur
                                          wrote on last edited by
                                          #20

                                          Should I paste all my code with pastbin SGalst ?

                                          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