GridLayout columnSpan property doesn't work as expected [duplicate] - qt

I'm trying to figure out a way to layout items proportionally by specifying a kind of weight for each item. For example the way Android does their layouts.
The way I'm trying to achieve it is like so:
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
GridLayout {
columns: 4
width: 640
height: 480
Rectangle {
color: "red"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 1
}
Rectangle {
color: "#80000000"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 2
}
Rectangle {
color: "blue"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 1
}
}
I would expect the width of the middle rectangle to be the sum of the other two rectangles, but instead they are all equal widths.
Using relational bindings on the Layout attached properties seems to always lead to weird binding loops. I know I could just use a Row instead with relational bindings, but I'd prefer to use Layouts if possible.
EDIT
This seems to work the way I want it to, but I don't know why it works. It behaves as if the preferredWidth value is the weight of the item.
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
RowLayout {
width: 640
height: 480
Rectangle {
color: "red"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredWidth: 1
}
Rectangle {
color: "#80000000"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredWidth: 2
}
Rectangle {
color: "blue"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredWidth: 1
}
}

Not sure if intentional or not but Layout.preferredWidth (or Layout.preferredHeight for ColumnLayouts) can be used as a "weight". Things break when also specifying Layout.minimumWidth, but I don't think it makes much sense to be specify minimum dimensions when trying to implement layouts in terms of weights anyways.
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
RowLayout {
width: 640
height: 480
Rectangle {
color: "red"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredWidth: 1
}
Rectangle {
color: "#80000000"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredWidth: 2
}
Rectangle {
color: "blue"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredWidth: 1
}
}

Related

Adapting the height of RowLayout as part of ColumnLayout

I have a simple question - is there a way I can make my rowLayout change its height evenly with the rest of columnLayout items? Or what exactly does it prevent from adjusting its height along with the rectangles?
A simple piece of code:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.5
ApplicationWindow {
visible: true
width: 640
height: 480
ColumnLayout {
anchors.fill: parent
anchors.margins: 30
RowLayout {
Layout.alignment: Qt.AlignHCenter
Layout.fillHeight: true
Rectangle {
id: rec1
width: 30; height: 30
Layout.alignment: Qt.AlignVCenter
color: "darkslateblue"
}
Label {
text: qsTr("Heading 1")
font.pixelSize: 25
verticalAlignment: Text.AlignVCenter
}
}
Rectangle {
id: rec2
Layout.fillWidth: true
Layout.fillHeight: true
color: "bisque"
}
Rectangle {
id: rec3
Layout.fillWidth: true
Layout.fillHeight: true
color: "bisque"
}
}
}
Right now I'm getting something like this:
One possible option is to use an Item as a RowLayout container:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.5
ApplicationWindow {
visible: true
width: 640
height: 480
ColumnLayout {
anchors.fill: parent
anchors.margins: 30
Item{
Layout.fillWidth: true
Layout.fillHeight: true
RowLayout {
Layout.alignment: Qt.AlignHCenter
anchors.fill: parent
Rectangle {
id: rec1
width: 30; height: 30
Layout.alignment: Qt.AlignVCenter
color: "darkslateblue"
}
Label {
text: qsTr("Heading 1")
font.pixelSize: 25
verticalAlignment: Text.AlignVCenter
}
}
}
Rectangle {
id: rec2
Layout.fillWidth: true
Layout.fillHeight: true
color: "bisque"
}
Rectangle {
id: rec3
Layout.fillWidth: true
Layout.fillHeight: true
color: "bisque"
}
}
}

Qt ColumnLayout full screen

Qt: 5.11.1
OS: Windows 10 64bit
This is Qt Quick application. when the window is in full screen mode. The layout becomes strange. I think it's an afterimage of the previous size.
startup
full screen
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ColumnLayout{
spacing: 2
anchors.fill: parent
Rectangle {
Layout.alignment: Qt.AlignCenter
color: "red"
Layout.preferredWidth: 40
Layout.preferredHeight: 40
}
Rectangle {
Layout.alignment: Qt.AlignRight
color: "green"
Layout.preferredWidth: 40
Layout.preferredHeight: 70
}
Rectangle {
Layout.alignment: Qt.AlignBottom
Layout.fillHeight: true
color: "blue"
Layout.preferredWidth: 70
Layout.preferredHeight: 40
}
Rectangle {
Layout.alignment: Qt.AlignBottom
Layout.fillHeight: true
color: "orange"
Layout.preferredWidth: 70
Layout.preferredHeight: 40
}
}
}
Not an answer, but a simplification of the problem
import QtQuick 2.11
import QtQuick.Window 2.11
Window {
visible: true
width: 640
height: 480
// Works with any Item
Rectangle {
anchors.centerIn: parent
color: "red"
width: 40
height: 40
}
}
How to reproduce
On Windows 10, click on the window's maximize button
Previous frame is not cleared
Could be related to https://bugreports.qt.io/browse/QTBUG-54451
New bug created https://bugreports.qt.io/browse/QTBUG-70570

Qt Quick layout changes on different events

When I execute my QML code, the output is:
When I minimize the window, It becomes like
and finally, when I again maximize the window it changes to
the GUI which I want to make looks like
![][5]
I am not getting what is the issue for all of the changes in GUI at different events. And this is the Qml code which I wrote
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
Window {
visible: true
width: 1080
height: 720
title: qsTr("Login")
GridLayout{
Rectangle{
id:one
Rectangle
{ id:two
color:"black";
width: 700
height:40
}
Image {
id: image
x: 470
y: 0
width: 54
height: 42
source: "qrc:/user.png"
}
Rectangle
{
id:three;
color:"#f47a42";
width: 200
height:40
anchors.left:two.right;
anchors.margins:940
Text {
id: user
text: qsTr("4200")
color:"white"
anchors.top: value.bottom
}
Text
{
id: value;
text: qsTr("User");
color:"yellow"
}}
}
}
Rectangle{
ColumnLayout{
width: 50
height: childrenRect.height+fillHeight;
}
color:"green"
}
}
So why this is happening and how can I solve this problem?
Output of the code below
Here is example of scalable window:
import QtQuick 2.11
import QtQuick.Window 2.11
import QtQuick.Layouts 1.11
Window {
visible: true
width: 800
height: 600
title: qsTr("Layout example")
ColumnLayout{
spacing: 0
anchors.fill: parent
Item {
id: titlebar
Layout.preferredHeight: 40
Layout.fillWidth: true
RowLayout {
anchors.fill: parent
spacing: 0
Rectangle {
Layout.fillWidth: true
Layout.fillHeight: true
color: "orange"
Text {
anchors.centerIn: parent
text: "Title"
}
}
Rectangle {
Layout.preferredWidth: 100
Layout.fillHeight: true
color: "lightgreen"
Text {
anchors.centerIn: parent
text: "Actions"
}
}
}
}
Rectangle {
id: content
Layout.fillHeight: true
Layout.fillWidth: true
color: "lightyellow"
Text {
anchors.centerIn: parent
text: "Content"
}
}
}
}

QML Layouts: How to give weights to items in a row or column layout?

I'm trying to figure out a way to layout items proportionally by specifying a kind of weight for each item. For example the way Android does their layouts.
The way I'm trying to achieve it is like so:
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
GridLayout {
columns: 4
width: 640
height: 480
Rectangle {
color: "red"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 1
}
Rectangle {
color: "#80000000"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 2
}
Rectangle {
color: "blue"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 1
}
}
I would expect the width of the middle rectangle to be the sum of the other two rectangles, but instead they are all equal widths.
Using relational bindings on the Layout attached properties seems to always lead to weird binding loops. I know I could just use a Row instead with relational bindings, but I'd prefer to use Layouts if possible.
EDIT
This seems to work the way I want it to, but I don't know why it works. It behaves as if the preferredWidth value is the weight of the item.
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
RowLayout {
width: 640
height: 480
Rectangle {
color: "red"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredWidth: 1
}
Rectangle {
color: "#80000000"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredWidth: 2
}
Rectangle {
color: "blue"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredWidth: 1
}
}
Not sure if intentional or not but Layout.preferredWidth (or Layout.preferredHeight for ColumnLayouts) can be used as a "weight". Things break when also specifying Layout.minimumWidth, but I don't think it makes much sense to be specify minimum dimensions when trying to implement layouts in terms of weights anyways.
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
RowLayout {
width: 640
height: 480
Rectangle {
color: "red"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredWidth: 1
}
Rectangle {
color: "#80000000"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredWidth: 2
}
Rectangle {
color: "blue"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredWidth: 1
}
}

Setting Layout.fillHeight: true make rectangle invisible In Qt Quick 2

I'm trying to create a basic layout for my application. I want it to look like this . This is my set up so far.
main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("basic layout")
ColumnLayout {
anchors.fill: parent
spacing: 0
Header {
Layout.fillWidth: true
height: 50
}
Body {
Layout.fillWidth: true
Layout.fillHeight: true
}
Footer {
Layout.fillWidth: true
height: 30
}
}
}
Header.qml
import QtQuick 2.7
import QtQuick.Layouts 1.3
Rectangle {
color: "red"
}
Footer.qml
import QtQuick 2.7
import QtQuick.Layouts 1.3
Rectangle {
color: "blue"
}
Body.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
Item {
RowLayout {
anchors.fill: parent
Rectangle {
color: "red"
width: 100
Layout.fillHeight: true
}
Rectangle {
color: "green"
width: 400
Layout.fillHeight: true
}
Rectangle {
color: "blue"
width: 400
Layout.fillHeight: true
}
Rectangle {
color: "black"
Layout.fillWidth: true
Layout.fillHeight: true
}
}
}
When it's ran In the, the Body section I get is nothing but white space. Rectangles are visible when i give them a size manually. I tried adding anchors.fill: parent to Item level in Body.qml. Still result is the same. What am i doing wrong?
You have to set rectangle's width in the RowLayout using
Layout.preferredWidth: 400
instead of
width: 400

Resources