我正在尝试编写简单的QML应用程序,该应用程序将包含许多按行组织的重复元素。结果将类似于以下所示:
目前,我有一段重复的代码可以实现此目标:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
import QtQuick.Controls.Material 2.3
import QtQuick.Layouts 1.12
ApplicationWindow {
Material.theme: Material.Dark
visible: true
width: 480
height: 640
title: qsTr("Hello world!")
id: root
GridLayout {
anchors.horizontalCenter: parent.horizontalCenter
columns: 3
Text {
color: "white"
text: "aaa"
font.family: "Ubuntu"
font.pixelSize: 20
}
SpinBox {
from: 0
to: 1000
stepSize: 1
editable: true
}
Rectangle {
width: 40
height: 20
color: "#F48FB1"
Text {
font.family: "Ubuntu"
font.pixelSize: 16
text: "ml"
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
}
}
Text {
color: "white"
text: "bbb"
font.family: "Ubuntu"
font.pixelSize: 20
}
SpinBox {
from: 0
to: 100
stepSize: 1
editable: true
}
Rectangle {
width: 40
height: 20
color: "#F48FB1"
Text {
font.family: "Ubuntu"
font.pixelSize: 16
text: "%"
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
}
}
Text {
color: "white"
text: "ccc"
font.family: "Ubuntu"
font.pixelSize: 20
}
SpinBox {
from: 0
to: 100
stepSize: 1
editable: true
}
Rectangle {
width: 40
height: 20
color: "#F48FB1"
Text {
font.family: "Ubuntu"
font.pixelSize: 16
text: "%"
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
}
}
}
}
但是,每一行都具有完全相同的结构,因此我尝试从代码中提取组件:
Item {
id: ingredient
property alias ingredientText: qualifier.text
property alias ingredientMaxValue: amount.to
property alias ingredientUnit: unit.text
RowLayout {
Text {
id: qualifier
color: "white"
text: "asdf"
font.family: "Ubuntu"
font.pixelSize: 20
}
SpinBox {
id: amount
from: 0
to: 1000
stepSize: 1
editable: true
}
Rectangle {
width: 40
height: 20
color: "#F48FB1"
Text {
id: unit
font.family: "Ubuntu"
font.pixelSize: 16
text: "unit"
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
}
}
}
}
不幸的是,当我尝试在主窗口中使用组件时,该组件未对齐并脱离了它的可见部分(最大化窗口后,我可以再次看到它):
ApplicationWindow {
Material.theme: Material.Dark
visible: true
width: 480
height: 640
title: qsTr("Hello world!")
id: root
GridLayout { // I know now it will not work properly for sure,
// but i tried using other layouts and alignment methods
// and it didn't help me at all
anchors.horizontalCenter: parent.horizontalCenter
columns: 3
Ingredient {
ingredientMaxValue: 100; ingredientText: "qwerty"; ingredientUnit: "kg"
}
}
}
问题是:如何使用“ Ingredient”组件实现第一张图片中的项目对齐?
答案 0 :(得分:1)
您遇到以下错误:
Ingredient.qml
import QtQuick 2.0
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.5
Item {
id: ingredient
property alias ingredientText: qualifier.text
property alias ingredientMaxValue: amount.to
property alias ingredientUnit: unit.text
width: rl.implicitWidth
height: rl.implicitHeight
RowLayout {
id: rl
Text {
id: qualifier
color: "white"
text: "asdf"
font.family: "Ubuntu"
font.pixelSize: 20
}
SpinBox {
id: amount
from: 0
to: 1000
stepSize: 1
editable: true
}
Rectangle {
width: 40
height: 20
color: "#F48FB1"
Text {
id: unit
font.family: "Ubuntu"
font.pixelSize: 16
text: "unit"
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
}
}
}
}
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
import QtQuick.Controls.Material 2.3
import QtQuick.Layouts 1.12
ApplicationWindow {
Material.theme: Material.Dark
visible: true
width: 480
height: 640
title: qsTr("Hello world!")
id: root
ColumnLayout{
width: parent.width
height: implicitHeight
Ingredient{
Layout.alignment: Qt.AlignHCenter
ingredientMaxValue: 100; ingredientText: "qwerty"; ingredientUnit: "kg"
}
Ingredient{
Layout.alignment: Qt.AlignHCenter
ingredientMaxValue: 100; ingredientText: "qwerty"; ingredientUnit: "kg"
}
Ingredient{
Layout.alignment: Qt.AlignHCenter
ingredientMaxValue: 100; ingredientText: "qwerty"; ingredientUnit: "kg"
}
}
}
答案 1 :(得分:1)
问题出在anchors中。您应该阅读有关它的信息,它将对将来有所帮助。您可以尝试使用锚点来为您选择最佳位置。
简要介绍一下锚点。锚点将自动将调整项目的位置设置到屏幕上。我也建议阅读有关Column,Row和Grid的信息。
ApplicationWindow {
Material.theme: Material.Dark
visible: true
width: 480
height: 640
title: qsTr("Hello world!")
id: root
GridLayout {
anchors.fill: parent
columns: 3
Item {
id: ingredient
anchors.fill: parent
property alias ingredientText: qualifier.text
property alias ingredientMaxValue: amount.to
property alias ingredientUnit: unit.text
RowLayout {
anchors.right: parent.right
anchors.left: parent.left
anchors.top: parent.top
Text {
id: qualifier
color: "white"
text: "asdf"
font.family: "Ubuntu"
font.pixelSize: 20
}
SpinBox {
id: amount
from: 0
to: 1000
stepSize: 1
editable: true
}
Rectangle {
width: 40
height: 20
color: "#F48FB1"
Text {
id: unit
font.family: "Ubuntu"
font.pixelSize: 16
text: "unit"
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
}
}
}
}
}
}