【问题标题】:How do I align my qml component properly?如何正确对齐我的 qml 组件?
【发布时间】:2019-09-23 06:33:30
【问题描述】:

我正在尝试编写简单的 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”组件实现第一张图片中的项目对齐?

【问题讨论】:

  • 组件的根 Item 没有大小,即它的大小为 0,因此布局以一种奇怪的方式放置它。除了这个项目在这里完全没用之外。

标签: qt layout qml qtquick2 qqmlcomponent


【解决方案1】:

您有以下错误:

  • 不以您孩子的尺寸来计算物品的尺寸。要计算正确的大小,您可以使用 RowLayout 的implicitWidth 和implicitHeight。
  • 您不能使用 GridLayout,因为您指出每个成分都是一个元素,在这种情况下您必须使用 ColumnLayout。

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
            }
        }
    }
}

ma​​in.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"
        }
    }

}

【讨论】:

    【解决方案2】:

    问题出在anchors。您应该阅读它,它将在将来对您有所帮助。您可以尝试使用锚点进行试验,以获取最适合您的定位。

    简单介绍一下锚点。锚点会自动设置项目调整到屏幕的位置。另外我建议阅读ColumnRowGrid

    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
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-03
      • 2019-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      • 2013-08-28
      • 1970-01-01
      相关资源
      最近更新 更多