为什么setLayoutDirection在我的Qt Quick演示中不起作用?

时间:2017-06-15 06:48:04

标签: qt qml

我想将我的演示布局设置为从右到左,我在main函数中设置它,如下所示:

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    app.setLayoutDirection(Qt::RightToLeft)
    return app.exec();
}

这是我的qml文件:

import QtQuick 2.6
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    id:root
    Row {
        spacing: 20
        Repeater {
            model: 5
            Rectangle {
                color: "red"
                opacity: (5 - index) / 5
                width: 70; height: 30

                Text {
                    text: index + 1+" hello"
                    width:parent.width
                }
            }
        }
    }
}

然而,布局结果仍然是从左到右: enter image description here

如何获得真正的RTL布局,所有组件都是从右到左,包含文本,就像这样: enter image description here

1 个答案:

答案 0 :(得分:1)

Right-to-left User Interfaces says that you should use the LayoutMirroring attached properties. Taking the example from that page:

import QtQuick 2.0

Rectangle {
    LayoutMirroring.enabled: true
    LayoutMirroring.childrenInherit: true

    width: 300; height: 50
    color: "yellow"
    border.width: 1

    Row {
        anchors { left: parent.left; margins: 5 }
        y: 5; spacing: 5

        Repeater {
            model: 5

            Rectangle {
                color: "red"
                opacity: (5 - index) / 5
                width: 40; height: 40

                Text {
                    text: index + 1
                    anchors.centerIn: parent
                }
            }
        }
    }
}