1

Sorry if this question is simple but I am reviewing the official documentation on how to set up a GridLayout with Buttons, Text, GroupBox and a ProgressBar in QML. The layout I am trying to achieve is the following below:

grid-layout

The problem I have is that I am struggling to achieve the layout above. I am having some problems understanding how to position the elements in the correct way inside the page.

What I have done so far:

1) I found a very useful example which I replicated successfully in order to understand the concept.

2) Also I came across this other source which helped because it explained and made clear some of the functions of the GridLayout but could still not solve completely the problem.

3) This source also helped but could not more forward.

4) In addition to the above points I also have been studying the offcial documentation. The official documentation is useful but still the components I am trying to position in the page are not properly set.

EDITS

The latest modification pushed me forward and now the lay-out it looks much better and close to what I am trying to achieve.

I figured out a way to use GridLayout (only because I still don't have a second choice that I can use with confidence) in a better way. The solution I found was to use a GroupBox for every entry I need, but I have some remaining problems as you can see from the EDITS I posted and I am not sure the cause of that, for example:

1) Why the TextField area is on the left while I used GridLayout with 2 columns? It seems that all components are pushed to one column only. I wanted the TextField(and related Text) to be on the center.

2) The first TextField carries correctly the text in the center. I did the same for the other texts but they are still on the left and am not sure what that is happening.

3) Why the first and the last button, respectively Button Test and Clear List occupy only, again, one column of the lay-out despite I used columns: 2 in the GridLayout and the goal would be that they both occupy the whole row.

4) I still don't see the ProgressBar despite I added it and am not sure why is possible to write in the TextField despite that should not be possible.

5) no errors from the debugger

confusing-layout

LATEST UPDATES & EDITS

Below the latest updates according to the latest comments and advise. I still have to solve a couple of remaining doubts:

1) The ProgressBar still looks very strange and does not extend according to the width of the window. For this I also went on the official documentation but still could not figure out why.

2) There is still no space between the Button Test and the top margin of the window.

3) GroupBox or the RowLayout does not extend for the width of the window. For this I consulted the following source which was useful for the ColumnLayout but it does not seem to apply elsewhere.

almost

The code I am using for this exercise is the following below, it is modified and you can only copy/paste and it will work:

main.qml

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.12
import QtQuick.Controls 1.4 as QQC1

ApplicationWindow {
    visible: true
    width: 640
    height: 520
    title: qsTr("GridLayout Example")
    id: testwindow

    ColumnLayout {
//        anchors.fill: parent
//        anchors.margins: 35
        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.alignment: parent.width
            spacing: 10
            GridLayout {
                width: parent.width
                columns: 1
                RowLayout {
                    spacing: 200
                    Layout.fillWidth: true
                    Layout.fillHeight: false
                    Label {
                        id: textField
                        text: "Eddie"
                        font.pointSize: 15
                    }
                    Text {
                        id: connected
                        text: "Not-Connected"
                        color: "red"
                        font.pointSize: 15
                        horizontalAlignment: Text.AlignRight
                    }
                }
            }
        }
        GroupBox {
            id: box2
            title: "Log-In Info"
            font.pointSize: 20
            width: parent.width
            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
                    }
                    Text {
                        id: logInStatus
                        text: "Logged In"
                        color: "red"
                        font.pointSize: 15
                        horizontalAlignment: Text.AlignRight
                    }
                }
            }
        }
        GroupBox {
            id: box3
            title: "Log-In ID"
            font.pointSize: 20
            width: parent.width
            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
                    }
                    Text {
                        id: loggedInAs
                        text: "Name"
                        color: "red"
                        font.pointSize: 15
                        horizontalAlignment: Text.AlignRight
                    }
                }
            }
        }
        GroupBox {
            id: box4
            title: "Email"
            font.pointSize: 20
            width: parent.width
            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
                    }
                    Text {
                        id: notSentEmail
                        text: "Not Sent"
                        color: "red"
                        font.pointSize: 15
                        horizontalAlignment: Text.AlignRight
                    }
                }
            }
        }
        Button {
            id: buttonClearList
            text: "Clear List"
            Layout.fillWidth: true
            font.pointSize: 20
        }
        QQC1.ProgressBar {
            ProgressBar {
                Layout.fillWidth: true

                anchors {
                    left: parent.left
                    right: parent.right
                    bottom: parent.bottom
                    leftMargin: -parent.leftMargin
                    rightMargin: -parent.rightMargin
                }
            }
        }

    }
}

How can I achieve the lay-out above? I have been working hard in the past couple of days to understand how to position the components correctly into the lay-out and studying the anchor property from the documentation. Thank you very much for pointing in the right direction and again, sorry if this question is a simple one.

Emanuele
  • 2,194
  • 6
  • 32
  • 71
  • It's unclear what are you asking. Please clarify your question. Please reorganize the question into a form: what I want to get - what have I done for this - what I got. – folibis Nov 25 '19 at 09:35
  • @folibis, thank you very much for taking the time to read my question. I reorganized the question in a much simpler way. Sorry if it looked confusing. The goal is achieving the lay-out on the print screen I posted but unfortunately I am having some problems in positioning the elements. – Emanuele Nov 25 '19 at 13:48
  • Uh, there's an error right at the top of your page.qml (which should be named "Page.qml") that prevents loading it at all (GridLayout has no `spacing` property). And _again_ you're not looking at the helpful debug warning messages Qt is giving you. For example it's clearly "reminding" that you can't use anchors in layouts, with a line # and everything. Or that you have a bunch of references to "red" which are undefined. And please fix the indents, you might spot something just by doing that. – Maxim Paperno Nov 25 '19 at 14:02
  • @MaximPaperno, thanks for your answer. I corrected the error but still no results. I made [this](https://www.dropbox.com/sh/bnmfl6u62oxspag/AACXCStWJZTL60WDjk6aS1pra?dl=0) maybe could be helpful? – Emanuele Nov 25 '19 at 14:24
  • I saw a potential related [question](https://stackoverflow.com/questions/29482970/what-is-the-difference-between-row-and-rowlayout). I wonder if I should consider everything as a row instead of using a `GridLayout`. – Emanuele Nov 25 '19 at 14:29
  • I don't see anything fixed in the post. I don't know what the video is supposed to be showing, though I can see the QML warnings from a previous session (so why ignore them?). Following examples is nice but pick one and understand it fully before trying to mix and match. Then try the other example you want to mix with and understand that first. Sorry, but you seem to be just skipping around between examples w/out understanding what is actually going on in them (how/why they work). From your desired results image I don't even see the need for a grid layout anywhere. – Maxim Paperno Nov 25 '19 at 14:45
  • Replace `Column` with `ColumnLayout`, `Column` has no attached properties. – folibis Nov 25 '19 at 16:23
  • @MaximPaperno, @folibis, after following your advice I achieved a much closer and better lay-out of my initial one. I took care of previous warnings, and now don't actually have warnings. I figured out a much better way to use `GridLayout` and `RowLayout`. I edited the post and it is much more simpler now and wrote down couple of noted under **EDITS**. I studied much more in depth the use of the components I used from documentation. I still have a couple of remaining doubts and am not sure if they are related on how to anchor components or something else. – Emanuele Nov 25 '19 at 20:07
  • Good improvements. Some observations: the `buttonTest` isn't in any layout, so the `Layout` attached property is useless and it is shown in the default position of `x: 0; y: 0` relative to its parent, which is the `ApplicationWindow`. If you want to leave space for it at the top, increase the top margin of your `Column`. The progress bar _top_ is anchored to the _bottom_ of the window, which is why you can't see it (it should be `bottom: parent.bottom`). I'm not sure why you're overlaying `Text` items into text edits... maybe you just want to use the `TextField::placeholderText` property. – Maxim Paperno Nov 25 '19 at 20:46
  • @MaximPaperno, thanks for the comments and the observations. I am about to complete the layout thanks to your advise. I still have left a couple of doubts and edited the question under **LATEST UPDATES & EDITS**. Thank you very much for your guidance and advise so far. – Emanuele Nov 25 '19 at 22:22
  • @MaximPaperno and folibis thank you all very much for your guidance. It took me some days of hard work and figure out the mechanism but I did it. Below I posted the updated solution! Thanks again! :) – Emanuele Nov 26 '19 at 15:27

1 Answers1

1

It took me some days of hard work, but I achieved the lay-out I was looking for, see below the print screen:

last-working-example

The complete working code is below if anyone needs:

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,0,0) }
                        GradientStop { position: 0.5; color: Qt.rgba(1,1,1,0.3) }
                        GradientStop { position: 1.0; color: Qt.rgba(0,0,0,0.05) }
                    }
                }
            }
            PropertyAnimation {
                target: control
                property: "value"
                from: 0
                to: 1
                duration: 5000
                running: true
                loops: Animation.Infinite
            }
        }

    }
}
Emanuele
  • 2,194
  • 6
  • 32
  • 71