我正在尝试为我的应用程序编写一个顶栏,其中应主要包含应用程序徽标(小图像)和应用程序标题(仅文本)。此外,我希望这个顶栏能够根据窗口的高度自动调整大小。
我是 QML 新手,但我想我应该将这些组件包装在Row
or a RowLayout https://blog.qt.io/blog/category/qt-quick-2/成分。这是我的示例代码:
import QtQuick 2.0
import QtQuick.Layouts 1.0
Rectangle
{
id: mainwindow
width: 1024
height: 600
Row
{
id: rowlayout
height: logoimage.height
spacing: 5
property int count: 3
anchors
{
left: parent.left
right: parent.right
top: parent.top
}
Image
{
id: logoimage
source: "qrc:/images/resources/images/icon.png"
height: mainwindow.height / 20
anchors.top: parent.top
anchors.left: parent.left
}
Text
{
id: logotext
text: qsTr("This is my logo text")
font.pixelSize: parent.height
font.family: "Sans Serif"
height: parent.height
verticalAlignment: Text.AlignVCenter
anchors.top: parent.top
anchors.left: logoimage.right
}
/*
Rectangle
{
id: otherrect
height: parent.height
color: "lightgreen"
anchors.top: parent.top
anchors.left: logotext.right
anchors.right: parent.right
}
*/
}
}
我告诉Row
组件的高度应遵循徽标的高度,并且Image
(logo) 组件,其高度应为 1/20Rectangle
(主窗口)组件。
Using a Row
容器,代码的行为符合预期,但我收到一个恼人的警告(QML Row: Cannot specify left, right, horizontalCenter, fill or centerIn anchors for items inside Row. Row will not function.
)而且我必须做很多锚定。相反,如果我使用RowLayout
容器,我可以移除大部分锚点,但是Image
完全忽略了它的height
属性(但文本仍然可以正确调整大小)。所以问题是:
- 这是一个错误吗
RowLayout
成分?我在用着Qt-5.1.0-测试版有Android支持,所以这可以是一个解释
- 我怎样才能使用
Row
组件而不在其子组件中使用锚点从而避免警告?
- 我错过了一些重要的东西,或者我几乎走在正确的轨道上,但我必须忍受这个 Qt 测试版,直到发布稳定版本?
您说您使用 Row 获得了预期的行为,因此您可能应该使用它。 Row 给出的警告要求您从其子元素中删除垂直锚点(顶部和底部)。
Row 元素为其子元素提供类似水平(左和右)锚点的行为,但它不介意您是否使用顶部和底部锚点(请注意,顶部和底部不在警告中)。
换句话说,从“logoimage”、“logotext”和“otherrect”中删除“anchors.left”和/或“anchors.right”行(如果您计划在某个时候取消注释),但不删除“anchors.top” ” 行,这应该停止警告并保持正确的行为。
另一种方法是仅删除 Row 元素并使用 Item 或 FocusScope(如果您计划在“顶栏”区域中包含输入元素),这不会尝试接管锚定操作,并且可能更适合您如果你真的喜欢主播。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)