[SOLVED]Translation and enum



  • 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


  • Lifetime Qt Champion

    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



  • 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!


  • Lifetime Qt Champion

    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.



  • 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);
    }
    

    }
    @



  • 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);@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.