Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Dynamic retranslate UI. Button gets not arranged properly
Qt 6.11 is out! See what's new in the release blog

Dynamic retranslate UI. Button gets not arranged properly

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 1 Posters 591 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
    sandro4912
    wrote on last edited by
    #1

    So I have this app were I can change in a settings window dynamically the language. Everything fine except one Issue:

    If I change from Spanish:

    f156daef-acf1-406c-8daf-99d2767f0a5f-image.png

    To german:

    f5194bf3-33de-4bf1-bafc-6c7d0c500b54-image.png

    You can See that the Button in the bottom right (saying SCHLIEßEN) is not arranged well.

    If I reopen the popup now issue it gets fixed:

    efd6392e-ae1a-4044-a001-91cae55fe513-image.png

    Is this some kind of bug we have to live with or is there a solution for this?

    Here is the code for this popup:

    import QtQuick 2.15
    import QtQuick.Layouts 1.15
    import QtQuick.Controls 2.15
    
    import LanguageSelectors 1.0
    
    Dialog {
        id: root
        x: 100
        y: 100
        width: 400
        height: 600
        modal: true
        focus: true
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent
    
        title: qsTr("Settings")
    
        property alias countOfQuestions: countOfQuestionsSpinBox.value
    
        property bool darkModeOn
    
        ColumnLayout {
            id: columnLayout
            RowLayout {
                Label {
                    text: qsTr("Light")
                    font.pointSize: 13.5
                }
                Switch {
                    id: colorModeSwitch
                    position: darkModeOn ? 1.0 : 0.0
    
                    onPositionChanged: {
                        if (position === 0.0) {
                            root.darkModeOn = false
                        } else {
                            root.darkModeOn = true
                        }
                    }
                }
                Label {
                    text: qsTr("Dark")
                    font.pointSize: 13.5
                }
            }
            RowLayout {
                Label {
                    text: qsTr("Count of Questions:")
                    font.pointSize: 13.5
                }
                SpinBox {
                    Layout.fillWidth: true
                    id: countOfQuestionsSpinBox
                    from: 0
                    to: 999
                    editable: true
                }
            }
            RadioButton {
                checked: LanguageSelector.language === LanguageSelector.German
                text: qsTr("German")
                onPressed: {
                    LanguageSelector.language = LanguageSelector.German
                }
            }
            RadioButton {
                checked: LanguageSelector.language === LanguageSelector.English
                text: qsTr("English")
                onPressed: {
                    LanguageSelector.language = LanguageSelector.English
                }
            }
            RadioButton {
                checked: LanguageSelector.language === LanguageSelector.Spanish
                text: qsTr("Spanish")
                onPressed: {
                    LanguageSelector.language = LanguageSelector.Spanish
                }
            }
        }
    
        standardButtons: Dialog.Close
    }
    

    I change the language by assigning to languageSelector:

    class LanguageSelector : public QObject {
        Q_OBJECT
        Q_PROPERTY(Language language READ language WRITE setLanguage NOTIFY
                       languageChanged)
    public:
        enum Language { German, English, Spanish };
        Q_ENUM(Language)
    
        explicit LanguageSelector(QObject *parent = nullptr);
    
        Language language() const;
        void setLanguage(Language newLanguage);
    
        QTranslator *getTranslator() const;
    
    private:
        QTranslator *mAppTranslator;
        QTranslator *mQtBaseTranslator;
        QTranslator *mQtQuickControlsTranslator;
        Language mLanguage;
    
        void loadGerman();
        void loadEnglish();
        void loadSpanish();
    
        void loadLanguage(const QLocale::Language &newLanguage);
    
    signals:
        void languageChanged();
    };
    

    LanguageSelector is registered to qml as a singleton:

    qmlRegisterSingletonInstance<LanguageSelector>(
        "LanguageSelectors", 1, 0, "LanguageSelector", languageSelector.get());
    

    And connected to retranslate like this:

    const QUrl url(QStringLiteral("qrc:/qml/main.qml"));
    QObject::connect(
        &engine, &QQmlApplicationEngine::objectCreated, &app,
        [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        },
        Qt::QueuedConnection);
    
    QObject::connect(languageSelector.get(), &LanguageSelector::languageChanged,
                     &engine, &QQmlApplicationEngine::retranslate);
    engine.load(url);
    

    Am I doing wrong here? Or is there some kind of trick to tell the button to get the proper position?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sandro4912
      wrote on last edited by
      #2

      Could it be that this is a bug that implicit width is not properly recalculated when the retranslate happens?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sandro4912
        wrote on last edited by
        #3

        I checked the issue and on Windows 10 it does not appear.

        Only on my System with Manjaro Linux with KDE Plasma it happens.

        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