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 and enum

[SOLVED]Translation and enum

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.1k 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.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    Hi guys,
    I'm building a custom QComboBox like this :

    @WorkoutComboBox::WorkoutComboBox(QWidget *parent) : QComboBox(parent) {

    /* generate the default list */
    insertItem(count(), "", QVariant() );
    insertItem(count(), Workout::getTypeToString(Workout::ENDURANCE), Workout::ENDURANCE);
    insertItem(count(), Workout::getTypeToString(Workout::TEMPO), Workout::TEMPO);
    insertItem(count(), Workout::getTypeToString(Workout::TEST), Workout::TEST);
    

    }
    @

    And I retrieve the QString value with this :
    @ QString Workout::getTypeToString(Type type) {
    if (type == Workout::ENDURANCE ) {
    return QApplication::translate("Workout", "Endurance", 0);
    }
    else if (type == Workout::TEMPO) {
    return QApplication::translate("Workout", "Tempo", 0);
    }
    else if (type == Workout::TEST) {
    return QApplication::translate("Workout", "Test_EN", 0);
    }
    else {
    return QApplication::translate("Workout", "Workout type not defined", 0);
    }
    }@

    The problem I face is that even after changing the langage, the QComboBox doesn't get updated (I guess it's because it's builded when I started the application?) Is there a way to force a translate ? Other code using "QString Workout::getTypeToString(Type type)" are getting correctly translated just not this QComboBox

    Thanks in advance


    Free Indoor Cycling Software - https://maximumtrainer.com

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

      Hi,

      You need to react on QEvent::LanguageChange and repopulate your combo box. The best would be to have a function e.g. updateUI that you call from the constructor to populate your box and that you also call from the event reimplementation you need to add to WorkoutComboBox

      Hope it helps

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

        The ui gets translated here in the mainWindow :
        ui->retranslateUi(this);
        the ComboBox is part of the mainWindow
        I'll try making a public function like you said and call it there.
        I tought maybe we could reimplement a function that would be called automaticly (retranslateUI)

        Thanks for your help!


        Free Indoor Cycling Software - https://maximumtrainer.com

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

          You should rather do it in the combo box class directly as I suggested and keep everything there. This will avoid you to have to call that public function every time you re-use your combo box and will keep a clean separation between your classes (why would your main window update a widget if he can do it himself ?). Designer based widgets are another story.

          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
          • M Offline
            M Offline
            maximus
            wrote on last edited by
            #5

            Thanks SGaist, working as expected!
            <3 QT

            @WorkoutComboBox::WorkoutComboBox(QWidget *parent) : QComboBox(parent) {

            updateUi();
            

            }

            /////////////////////////////////////////////////////////////////////////////////////////////////
            void WorkoutComboBox::updateUi() {

            this->clear();
            insertItem(count(), "", QVariant() );
            insertItem(count(), Workout::getTypeToString(Workout::ENDURANCE), Workout::ENDURANCE);
            insertItem(count(), Workout::getTypeToString(Workout::TEMPO), Workout::TEMPO);
            insertItem(count(), Workout::getTypeToString(Workout::TEST), Workout::TEST);
            

            }

            /////////////////////////////////////////////////////////////////////////////////////////////////
            void WorkoutComboBox::changeEvent(QEvent *event) {

            if (event->type() == QEvent::LanguageChange) {
                qDebug() << "LANGUAGE CHANGED!";
                updateUi();
            }
            else {
                QWidget::changeEvent(event);
            }
            

            }
            @


            Free Indoor Cycling Software - https://maximumtrainer.com

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maximus
              wrote on last edited by
              #6

              Forgot to mention, may be usefull for someone

              If you want to sort your QComboBox,
              I recommend addItem instead of insertItem so that you can use model().sort on your ComboBox

              @addItem(UtilPowerCurve::getTrainerCurveToString(UtilPowerCurve::KINETIC_ROAD_MACHINE), UtilPowerCurve::KINETIC_ROAD_MACHINE);
              

              addItem(UtilPowerCurve::getTrainerCurveToString(UtilPowerCurve::TEST_BIDON), UtilPowerCurve::TEST_BIDON);
              this->model()->sort(Qt::AscendingOrder);@


              Free Indoor Cycling Software - https://maximumtrainer.com

              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