如何在QML中使用按钮,GroupBox,文本和ProgressBar设置基本的GridLayout

很抱歉,如果这个问题很简单,但是我正在查看有关如何通过GridLayoutButtonsText和{{1 GroupBox中的}}。 我正在尝试实现的布局如下:

如何在QML中使用按钮,GroupBox,文本和ProgressBar设置基本的GridLayout

问题,我正在努力实现上述布局。 我在理解如何在页面内以正确的方式放置元素时遇到一些问题。

我到目前为止所做的事情:

1)我发现了一个非常有用的example,为了理解这个概念,我成功地复制了它。

2)另外,我遇​​到了this other source,因为它可以解释并阐明ProgressBar的某些功能,但仍不能完全解决问题,因此很有帮助。

3)这个source也有所帮助,但无法继续前进。

4)除了上述几点,我还研究了offcial documentation。官方文档很有用,但是我尝试在页面中放置的组件仍然设置不正确。

编辑

最新的修改推动了我前进,现在它的布局看起来更好并且接近于我要实现的目标。

我想出了一种使用QML的方法(只是因为我仍然没有第二种选择可以放心使用),因此可以更好地使用它。我发现的解决方案是为需要的每个条目使用一个GridLayout,但是从我发布的 EDITS 可以看出,我还有一些其他问题,我不确定造成该问题的原因,例如:

1 )为什么我在两列中使用GridLayoutGroupBox区域在左侧?似乎所有组件都只推送到一列。 我希望TextField(和相关的GridLayout)位于中心。

2)第一个TextField正确地将文本放在中间。我对其他文本也做了同样的操作,但是它们仍然在左侧,不确定发生了什么。

3)尽管我使用了{{1},为什么第一个按钮和最后一个按钮分别TextTextField仅占据布局的一列Button Test中的}},目标是它们都占据整行。

4)尽管我添加了Clear List,但仍然看不到它,并且不确定为什么columns: 2中可以写GridLayout

5)调试器没有错误

如何在QML中使用按钮,GroupBox,文本和ProgressBar设置基本的GridLayout

最新更新和编辑

根据最新评论并提供最新更新。 我仍然需要解决几个尚存的疑问:

1) ProgressBar看起来仍然很奇怪,并且不会根据窗口的宽度进行扩展。为此,我也进行了official documentation,但仍然不知道为什么。

2)TextField和窗口顶部之间仍然没有空格。

3) ProgressBarButton Test不在窗口的宽度范围内延伸。为此,我咨询了以下source,它对GroupBox很有用,但似乎不适用于其他地方。

如何在QML中使用按钮,GroupBox,文本和ProgressBar设置基本的GridLayout

我在本练习中使用的代码如下,已对其进行了修改,您只能复制/粘贴,并且可以正常工作:

main.qml

RowLayout

如何实现上述布局?在过去的几天中,我一直在努力工作,以了解如何正确地将组件放置到布局中,并从文档中研究ColumnLayout属性。 非常感谢您指出正确的方向,如果这个问题很简单,对不起。

wkao0705120104 回答:如何在QML中使用按钮,GroupBox,文本和ProgressBar设置基本的GridLayout

我花了几天的时间努力,但我实现了所需的布局,请参见打印屏幕下方:

last-working-example

完整的工作代码如下:

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.12
import QtGraphicalEffects 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 520
    title: qsTr("GridLayout Example")
    id: testwindow
    ColumnLayout {
        anchors.topMargin: 350 // margin from top of the page
        anchors.fill: parent
        spacing: 10
        //width: parent.width
        Button {
            id: buttonTest
            text: "Button Test"
            Layout.fillWidth: true
            font.pointSize: 20
        }
        GroupBox {
            id: box1
            title: "Connection"
            font.pointSize: 20
            Layout.fillWidth: true
            spacing: 10
            GridLayout {
                width: parent.width
                columns: 1
                RowLayout {
                    spacing: 200
                    Layout.fillWidth: true
                    Layout.fillHeight: false
                    Label {
                        id: textField
                        text: "Connection:"
                        font.pointSize: 15
                        Layout.fillWidth: true
                    }
                    Text {
                        id: connected
                        text: "Not-Connected"
                        color: "red"
                        font.pointSize: 15
                        horizontalAlignment: Text.AlignRight
                        Layout.fillWidth: true
                    }
                }
            }
        }
        GroupBox {
            id: box2
            title: "Log-In Info"
            font.pointSize: 20
            Layout.fillWidth: true

            spacing: 10
            GridLayout {
                width: parent.width
                columns: 1
                RowLayout {
                    spacing: 200
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    Layout.alignment: parent.width
                    Label {
                        id: textField3
                        text: "Status:"
                        font.pointSize: 15
                        Layout.fillWidth: true
                    }
                    Text {
                        id: logInStatus
                        text: "Not Logged-In"
                        color: "red"
                        font.pointSize: 15
                        horizontalAlignment: Text.AlignRight
                        Layout.fillWidth: true
                    }
                }
            }
        }
        GroupBox {
            id: box3
            title: "Log-In ID"
            font.pointSize: 20
            Layout.fillWidth: true

            spacing: 10
            GridLayout {
                width: parent.width
                columns: 1
                RowLayout {
                    spacing: 200
                    Layout.fillWidth: true;
                    Layout.fillHeight: true
                    Label {
                        id: textField5
                        text: "Logged in as:"
                        font.pointSize: 15
                        Layout.fillWidth: true
                    }
                    Text {
                        id: loggedInAs
                        text: "Name"
                        color: "red"
                        font.pointSize: 15
                        horizontalAlignment: Text.AlignRight
                        Layout.fillWidth: true
                    }
                }
            }
        }
        GroupBox {
            id: box4
            title: "Email"
            font.pointSize: 20
            Layout.fillWidth: true

            spacing: 10
            GridLayout {
                width: parent.width
                columns: 1
                RowLayout {
                    spacing: 200
                    Layout.fillWidth: true;
                    Layout.fillHeight: true
                    Label {
                        id: textField7
                        text: "Email:"
                        font.pointSize: 15
                        Layout.fillWidth: true
                    }
                    Text {
                        id: notSentEmail
                        text: "Not Sent"
                        color: "red"
                        font.pointSize: 15
                        horizontalAlignment: Text.AlignRight
                        Layout.fillWidth: true
                    }
                }
            }
        }
        Button {
            id: buttonClearList
            text: "Clear List"
            Layout.fillWidth: true
            font.pointSize: 20
        }
        ProgressBar {
            id: control

            Layout.fillWidth: true
            value: 0
            height: 20

            clip: true
            background: Rectangle {
                implicitWidth: 200
                implicitHeight: 20 // it was 6
                border.color: "#999999"
                radius: 5
            }
            contentItem: Item {
                implicitWidth: 200
                implicitHeight: 20 // it was 4

                Rectangle {
                    id: bar
                    width: control.visualPosition * parent.width
                    height: parent.height
                    radius: 5
                }
                LinearGradient {
                    anchors.fill: bar
                    start: Qt.point(0,0)
                    end: Qt.point(bar.width,0)
                    source: bar
                    gradient: Gradient {
                        GradientStop { position: 0.0; color: "#17a81a" }
                        GradientStop { id: grad; position: 0.5; color: Qt.lighter("#17a81a",2) }
                        GradientStop { position: 1.0; color: "#17a81a" }
                    }
                    PropertyAnimation {
                        target: grad
                        property: "position"
                        from: 0.1
                        to: 0.9
                        duration: 1000
                        running: true
                        loops: Animation.Infinite
                    }
                }
                LinearGradient {
                    anchors.fill: bar
                    start: Qt.point(0,0)
                    end: Qt.point(0,bar.height)
                    source: bar
                    gradient: Gradient {
                        GradientStop { position: 0.0; color: Qt.rgba(0,0) }
                        GradientStop { position: 0.5; color: Qt.rgba(1,1,0.3) }
                        GradientStop { position: 1.0; color: Qt.rgba(0,0.05) }
                    }
                }
            }
            PropertyAnimation {
                target: control
                property: "value"
                from: 0
                to: 1
                duration: 5000
                running: true
                loops: Animation.Infinite
            }
        }

    }
}
本文链接:https://www.f2er.com/3039729.html

大家都在问