javascript 如何将组件定位在我所有 QML 视图的顶部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9140549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:42:52  来源:igfitidea点击:

How to position a component at the top of all my QML views

javascriptqtqml

提问by Kobojunkie

I have a QML Menu component that I would like to place at the top of each and everyone of my views and sort of sync up so it goes the look and feel of the typical webpage menu. How do I go about doing this please

我有一个 QML 菜单组件,我想将它放在我的每个视图的顶部并进行同步,以便它具有典型网页菜单的外观和感觉。请问我该怎么做

import QtQuick 1.0 

Row {
    width: 500
    height: 50
    anchors.centerIn: parent
    spacing: parent.width/6

    Button {
         id: loadButton
         text: qsTr("Menu")
    }
    Button { 
         id: saveButton
         text: qsTr("Profile")
    }
    Button {
         id: exitButton
         text: qsTr("View Products")
    }

}

回答by muenalan

Read about "z" property QDeclarativeItem or QML Item. The answer is to give your menu component instance the highest z value.

阅读“z”属性 QDeclarativeItem 或 QML Item。答案是给你的菜单组件实例最高的 z 值。

MyMenu
{
   z: 100

   ...
}

Rectangle
{
   z: 1

   ...
}

The qml renderer decides upon this values which items to draw first; and in case of something overlaying your menu it will end below it. By the way, the default z value is 0, so all are same and it is more or less undefined in which order it is rendered.

qml 渲染器根据这个值决定首先绘制哪些项目;如果菜单上有东西覆盖,它将在菜单下方结束。顺便说一下,默认的 z 值是 0,所以所有都是相同的,并且或多或少地未定义它的呈现顺序。

Good Luck!

祝你好运!

回答by Wes Hardaker

With QML everything needs to end up in a container in order to align everything with it. In this case, if you're trying to build a bunch of Buttons into a menu/row then you need that to always be at the top of every page-like container you put it in.

使用 QML,一切都需要放在一个容器中,以便将所有内容与它对齐。在这种情况下,如果您尝试将一堆按钮构建到菜单/行中,那么您需要将其始终位于您放入的每个类似页面的容器的顶部。

IE, put your above stuff all in a file called "Menu.qml". Then inside your program every place you want the menu to appear, make an enclosing Rectangle (or whatever) and anchor the menu to the top:

IE,把你上面的东西都放在一个名为“Menu.qml”的文件中。然后在你的程序中你希望菜单出现的每个地方,制作一个封闭的矩形(或其他)并将菜单锚定到顶部:

Rectangle {
    Menu { id: menu; anchor.top: parent.top; anchor.left: parent.left; }

    // put other stuff here
    Rectangle { anchor.top: menu.bottom; }
 }

If you do that for every object that will appear then you're good to go!

如果您对每个将出现的对象都这样做,那么您就可以开始了!

As another example, in a PageStack, make each page include that at the top.

再举一个例子,在 PageStack 中,让每个页面都包含在顶部。