简体   繁体   English

如何在 rtl 对齐中打开右侧的子菜单?

[英]How to open sub-menu on right hand side in rtl alignments?

I have a menu that consists of Items and sub-menus.我有一个由项目和子菜单组成的菜单。 the texts and titles of items should be shown from right to left.项目的文本和标题应从右到左显示。 I achieved this by using LayoutMirroring.enabled = true .我通过使用LayoutMirroring.enabled = true实现了这一点。

    Menu {
        id: right_menu
        property var rtl: true;

        MenuItem {
            text: "گزینه ۱"
            LayoutMirroring.enabled: true
            LayoutMirroring.childrenInherit: true
        }

        Menu {
            title: "تنظیمات"

            MenuItem {
                LayoutMirroring.enabled: true
                LayoutMirroring.childrenInherit: true
                text: "گزینه ۱"
            }

            MenuItem {
                LayoutMirroring.enabled: true
                LayoutMirroring.childrenInherit: true
                text: "گزینه ۱"
            }
        }

        delegate: MenuItem {
            LayoutMirroring.enabled: true
            LayoutMirroring.childrenInherit: true
        }
    }

This is the output:这是输出:

错误的输出

However, this is not a valid output and I want to force the Menu to open subMenu on the right-hand side instead of left.但是,这不是有效的输出,我想强制菜单在右侧而不是左侧打开子菜单。 this is the output I'm looking for:这是我正在寻找的输出:

预期产出

How can I achieve this?我怎样才能做到这一点?

You can use the overlap property to shift the submenu over to the position you want.您可以使用overlap属性将子菜单移动到您想要的位置。

Note that if the position of the menu is too close to the left or right edge of the window, it will still open on the opposite side.请注意,如果菜单的位置太靠近窗口的左边缘或右边缘,它仍然会在相反的一侧打开。

Menu {
    id: right_menu
    property var rtl: true;

    MenuItem {
        text: "گزینه ۱"
        LayoutMirroring.enabled: true
        LayoutMirroring.childrenInherit: true
    }

    Menu {
        title: "تنظیمات"
        overlap: width * 2  // <<-- Shift the submenu over

        MenuItem {
            LayoutMirroring.enabled: true
            LayoutMirroring.childrenInherit: true
            text: "گزینه ۱"
        }

        MenuItem {
            LayoutMirroring.enabled: true
            LayoutMirroring.childrenInherit: true
            text: "گزینه ۱"
        }
    }

    delegate: MenuItem {
        LayoutMirroring.enabled: true
        LayoutMirroring.childrenInherit: true
    }
}

Thanks to @JarMan, his solution solves the problem if one just wants to rotate the menu(as is in my case).感谢@JarMan,如果人们只想旋转菜单(就我而言),他的解决方案可以解决问题。 But a better solution for right to left languages is to make your whole app right to left;但是对于从右到左的语言来说,更好的解决方案是让你的整个应用从右到左; because in right to left languages, everything should be in this fashion.因为在从右到左的语言中,一切都应该以这种方式进行。 To do this, you can add the following line in the main.cpp file of your project.为此,您可以在项目的main.cpp文件中添加以下行。

app.setLayoutDirection(Qt::rightToLeft);

and then in any other Item or window, add the following lines:然后在任何其他项目或窗口中,添加以下行:

LayoutMirroring.enabled: Qt.application.layoutDirection == Qt.RightToLeft
LayoutMirroring.childrenInherit: Qt.application.layoutDirection == Qt.RightToLeft

I suspect LayoutMirroring is causing your submenu to popUp in the wrong position.我怀疑 LayoutMirroring 导致您的子菜单在错误的位置弹出。 Instead of using LayoutMirroring to achieve the text format you'd like, use a custom text component and edit the alignment of the text there.不要使用 LayoutMirroring 来实现您想要的文本格式,而是使用自定义文本组件并在那里编辑文本的对齐方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM