ColumnLayout inside ScrollView doesn't resize its content - qt

Here's how it looks:
Both 3rd and 4th Card are laid out with ColumnLayout. The only difference is ColumnLayout of the 4th Card is inside ScrollView. Here's the main.qml:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.3
Window {
visible: true
width: 640
height: 480
title: "Test Window"
GridLayout{
anchors.fill: parent
columns: 2
rows: 2
Card{
header: "H1"
ColumnLayout{
Text{text: "text 1"}
Text{text: "text 2"}
Text{text: "text 3"}
}
}
Card{
header: "H2"
Text{text: "text 2"}
}
Card{
header: "H3"
ColumnLayout{
Field{
label: "Name"
placeholder: "name"
}
Field{
label: "Age"
placeholder: "age"
}
}
}
Card{
header: "H4"
ScrollView{
/*
anchors.fill: parent
Layout.fillWidth: true
Layout.fillHeight: true
Layout.minimumWidth: parent.width
Layout.preferredWidth: parent.width
contentWidth: parent.width
width: parent.width
*/
clip: true
ColumnLayout{
//anchors.fill: parent
Field{
label: "Name"
placeholder: "name"
}
Field{
label: "Age"
placeholder: "age"
}
Field{
label: "Address"
placeholder: "address"
}
Field{
label: "Age"
placeholder: "age"
}
Field{
label: "Address"
placeholder: "address"
}
}
}
}
}
}
I've tried with everything commented out in 4th Card, none of those helps! here's the Card.qml:
import QtQuick 2.9
import QtQuick.Controls 2.5
import QtGraphicalEffects 1.0
import QtQuick.Layouts 1.3
Item {
default property alias content: content.children /*content.data*/
property alias header: header.text
property double margin: 10
property double separatorMargin: 5
Layout.fillHeight: true
Layout.fillWidth: true
Layout.margins: 10
Rectangle{
id: rect
anchors.fill: parent
ColumnLayout{
anchors.fill: parent
Text{
id: header
font.bold: true
leftPadding: margin
topPadding: margin
}
Rectangle{
id: separator
Layout.fillWidth: true
Layout.leftMargin: separatorMargin
Layout.rightMargin: separatorMargin
height: 2
border.color: "black"
}
StackLayout {
id: content
Layout.preferredHeight: parent.height
Layout.leftMargin: margin
Layout.rightMargin: margin
}
}
}
DropShadow{
anchors.fill: rect
source: rect
radius: 10
samples: 15
color: "black"
}
}
and here's Field.qml:
import QtQuick 2.9
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.3
RowLayout{
property alias label: label.text
property alias placeholder: field.placeholderText
Label{
id: label
Layout.minimumWidth: 50
}
TextField{
id: field
Layout.fillWidth: true
Layout.preferredHeight: 30
font.pointSize: 12
verticalAlignment: Qt.AlignVCenter
}
}

I don't know why do I get some answers after posting a question!
This has a solution, I'd to add this:
width: Math.max(implicitWidth, scrollview.availableWidth)
inside ColumnLayout.
EDIT
This doesn't work if I add:
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
Here's how it looks with horizontal bar turned off:

Related

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. But as you can see in the pictures...
Window after starting
Window after downsizing
... the properties will not scale properly according to the size of the 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. 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.

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

QML wrap buttons in narrow vs wide layout

How can i make the buttons wrap when a dialog window is resized to be narrow by the user? Currently they are just cutoff.
QML
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
Frame {
width: parent.width
height: parent.height
ColumnLayout {
width: implicitWidth
spacing: 20
anchors.left: parent.left
anchors.right: parent.right
Layout.alignment: Qt.AlignTop
// Config
ColumnLayout {
Layout.fillWidth: true
spacing: 2
Label {
text: "Config"
font.bold: true
}
TextField {
readOnly: true
placeholderText: qsTr("Path to config.json file (C:\\desktop\\config.txt)")
Layout.fillWidth: true
}
RowLayout {
Layout.fillWidth: true
Layout.alignment: Qt.AlignRight
Button {
text: qsTr("Edit")
implicitWidth: implicitHeight
}
Button {
text: qsTr("Browse")
implicitWidth: implicitHeight
}
Button {
text: qsTr("Clear")
implicitWidth: implicitHeight
}
Button {
text: qsTr("Find")
implicitWidth: implicitHeight
}
}
}
// File
ColumnLayout {
Layout.fillWidth: true
spacing: 2
Label {
text: "File"
font.bold: true
}
TextField {
readOnly: true
placeholderText: qsTr("Path to config.json file (C:\\desktop\\file.txt)")
Layout.fillWidth: true
}
RowLayout {
Layout.fillWidth: true
Layout.alignment: Qt.AlignRight
Button {
text: qsTr("Clear")
implicitWidth: implicitHeight
}
Button {
text: qsTr("Find")
implicitWidth: implicitHeight
}
}
}
}
}
main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Page1 {
anchors.centerIn: parent
}
}
You have to use Flow instead of RowLayout:
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
Frame {
width: parent.width
height: parent.height
ColumnLayout {
width: implicitWidth
spacing: 20
anchors.left: parent.left
anchors.right: parent.right
Layout.alignment: Qt.AlignTop
// Config
ColumnLayout {
Layout.fillWidth: true
spacing: 2
Label {
text: "Config"
font.bold: true
}
TextField {
readOnly: true
placeholderText: qsTr("Path to config.json file (C:\\desktop\\config.txt)")
Layout.fillWidth: true
}
Flow {
Layout.fillWidth: true
layoutDirection: Qt.RightToLeft
Button {
text: qsTr("Edit")
implicitWidth: implicitHeight
}
Button {
text: qsTr("Browse")
implicitWidth: implicitHeight
}
Button {
text: qsTr("Clear")
implicitWidth: implicitHeight
}
Button {
text: qsTr("Find")
implicitWidth: implicitHeight
}
}
}
// File
ColumnLayout {
Layout.fillWidth: true
spacing: 2
Label {
text: "File"
font.bold: true
}
TextField {
readOnly: true
placeholderText: qsTr("Path to config.json file (C:\\desktop\\file.txt)")
Layout.fillWidth: true
}
Flow {
Layout.fillWidth: true
layoutDirection: Qt.RightToLeft
Button {
text: qsTr("Clear")
implicitWidth: implicitHeight
}
Button {
text: qsTr("Find")
implicitWidth: implicitHeight
}
}
}
}
}

center a checkbox in tableview with QML

I am using a checkbox control in a TableView, which I define with QML as follows:
import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
TableViewColumn {
title: ""
role: "check"
delegate: CheckBox {
anchors.fill: parent
checked: false
anchors { horizontalCenter: parent.horizontalCenter }
}
}
I can use it in a TableView as follows:
import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
ControlView {
visible: true
width: parent.width; height: parent.height
anchors.centerIn: parent
TableView {
id: reviewTable
width: parent.width; height: parent.height
anchors.centerIn: parent
TableViewCheckBoxColumn {
title: "Upload"
width: 100
resizable: false
}
TableViewColumn {
resizable: true
role: "Dummy"
title: "Dummy"
width: 250
}
style: TableViewStyle {
headerDelegate: Rectangle {
height: textItem.implicitHeight
width: textItem.implicitWidth
Text {
id: textItem
anchors.fill: parent
verticalAlignment: Text.AlignVCenter
text: styleData.value
renderType: Text.NativeRendering
font.bold: true
}
}
}
model: ListModel {
ListElement {
Dummy: "value 1"
}
ListElement {
Dummy: "value 3"
}
}
}
}
Now despite having set the anchors (or any alignment property that were available), the checkbox remains aligned to the left. How can I center it horizontally wrt to the column?
Wrap the CheckBox in the delegate as below:
Item {
anchors.fill: parent
CheckBox {
anchors.centerIn: parent
checked: false
}
}
And btw, don't mix QtQuick versions in the same application.

MouseArea inside Flickable/TextEdit doesn't pass through mouse events using propagateComposedEvents

import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.1
ApplicationWindow {
flags: Qt.FramelessWindowHint
width: 500
height: 500
x: (Screen.width - width) / 2
y: (Screen.height - height) / 2
color: "black"
opacity: 0.8
Flickable {
anchors.fill: parent
contentWidth: html.paintedWidth
contentHeight: html.paintedHeight
boundsBehavior: Flickable.StopAtBounds
TextEdit {
id: html
objectName: "html"
anchors.fill: parent
textFormat: TextEdit.RichText
focus: true
Keys.onEscapePressed: Qt.quit()
font.family: "Droid Sans Mono"
font.pointSize: 11
selectByMouse: true
readOnly: true
MouseArea {
anchors.fill: parent
propagateComposedEvents: true
onClicked: {
console.log("clicked")
mouse.accepted = false
}
}
}
}
}
I'm unable to get "clicked" printed... it seems like propagateComposedEvents and mouse.accepted are just not working as expected.
I'm using Qt 5.3 Beta.
contentWidth/Height is wrong,
import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.1
ApplicationWindow {
flags: Qt.FramelessWindowHint
width: 500
height: 500
x: (Screen.width - width) / 2
y: (Screen.height - height) / 2
//color: "black"
opacity: 0.8
visible: true
Flickable {
anchors.fill: parent
//contentWidth: html.paintedWidth
//contentHeight: html.paintedHeight
boundsBehavior: Flickable.StopAtBounds
TextEdit {
id: html
objectName: "html"
anchors.fill: parent
textFormat: TextEdit.RichText
focus: true
Keys.onEscapePressed: Qt.quit()
font.family: "Droid Sans Mono"
font.pointSize: 11
selectByMouse: true
readOnly: true
text: "hello world"
MouseArea {
anchors.fill: parent
propagateComposedEvents: true
onClicked: {
console.log("clicked")
mouse.accepted = false
}
}
}
}
}

Resources