简体   繁体   English

如何使我的 GroupBox 布局在 QML 中可缩放?

[英]How can I make my GroupBox Layouts scalable in QML?

I have the following problem.我有以下问题。 I created a Periodic table in QML that consists of GroupBoxes that contain a ColumnLayout in order to arrange the specific properties of each element.我在 QML 中创建了一个周期表,它由包含 ColumnLayout 的 GroupBox 组成,以便安排每个元素的特定属性。 But as you can see in the pictures...但是正如你在图片中看到的那样......

Window after starting启动后Window

Window after downsizing减员后Window

... the properties will not scale properly according to the size of the GroupBox. ...属性不会根据 GroupBox 的大小正确缩放。 So when I scale down the size of the window, some text properties will just move out of its GroupBox and lay wherever they are not supposed to be.因此,当我缩小 window 的大小时,一些文本属性将移出其 GroupBox 并放置在它们不应该出现的位置。 How can I fix this?我怎样才能解决这个问题?

//PeriodicTable.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Material 2.15
import QtQuick.Layouts 1.15

Item {
    id: root
    Button {
        id: button
        checkable: true
        text: qsTr("Show")

        onClicked: window.show()
    }
    Window {
        id: window
        Material.accent: parent.Material.accent
        Material.background: parent.Material.background
        Material.foreground: parent.Material.foreground
        Material.primary: parent.Material.primary
        Material.theme: parent.Material.theme
        color: Material.background
        height: parent.height
        title: qsTr("Periodic table")
        width: parent.width

        GridView {
            id: gridview
            anchors.fill: parent
            cellHeight: cellWidth * 1.5
            cellWidth: parent.width / 18

            delegate: PeriodicTableDelegate {
            }
            model: PeriodicTableModel {
            }
        }
    }
}

//PeriodicTableModel.qml
import QtQuick 2.15
import QtQuick.Controls 2.15

ListModel {
    id: elements
    ListElement {
        atomicWeight: "1.00794"
        electronConfiguration: qsTr("1s")
        elementName: qsTr("Hydrogen")
        elementSign: qsTr("H")
        ionizationEnergy: qsTr("13 5984")
        ordinalNumber: qsTr("1")
        unknownNumber: qsTr("S1/2")
    }
} // I shortened this to just one element because yet it is the only one I have

// PeriodicTableDelegate.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

GroupBox {
    property int cellHeight: GridView.view.cellHeight
    property int cellWidth: GridView.view.cellWidth

    height: cellHeight
    width: cellWidth

    ColumnLayout {
        RowLayout {
            Label {
                Layout.alignment: Qt.AlignLeft
                Layout.fillWidth: true
                font.bold: true
                text: ordinalNumber
            }
            Label {
                Layout.alignment: Qt.AlignRight
                text: unknownNumber
            }
        }
        Label {
            Layout.alignment: Qt.AlignHCenter
            font.bold: true
            text: elementSign
        }
        Label {
            Layout.alignment: Qt.AlignHCenter
            text: elementName
        }
        Label {
            Layout.alignment: Qt.AlignHCenter
            text: atomicWeight
        }
        Label {
            Layout.alignment: Qt.AlignHCenter
            text: electronConfiguration
        }
        Label {
            Layout.alignment: Qt.AlignHCenter
            text: ionizationEnergy
        }
    }
}





a slightly contrived example:一个稍微做作的例子:

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.0

Window {
    height: 800
    width: 600
    visible: true
    title: qsTr("cell test")

    GridView {
        id: grid
        anchors.fill: parent
        cellHeight: grid.height / 3
        cellWidth: grid.width / 4
        model: 12
        delegate: Rectangle {
            id: rect
            width: grid.cellWidth
            height: grid.cellHeight
            color: Qt.rgba(Math.random(), Math.random(), Math.random(), 1)

            ColumnLayout {
                id: layout
                anchors.fill: parent
                Repeater {
                    model: 5
                    Text {
                        id: txt
                        property int fsize: (layout.height + layout.width) / 30
                        Layout.fillWidth: true
                        text: "some text"
                        horizontalAlignment:Text.AlignHCenter
                        font.pointSize: fsize > 0 ? fsize : 16

                    }
                }
            }
        }
    }
}

The ColumnLayout in your GroupBox is missing an anchors.fill: parent .您的 GroupBox 中的 ColumnLayout 缺少 anchors.fill anchors.fill: parent

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

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