QML ScrollView inside Item won't scroll - qt

I want to have a fixed header for a ScrollView, so I thought that doing this would work:
Item {
Rectangle {
---i want to be a fixed header
}
ScrollView {
---list items
}
}
Basically inside the Item I want a Rectangle or Row to act as header and below that a ScrollView.
However, if I put the ScrollView inside an Item (or Rectangle, I tried already), nothing will scroll - the scrollbars do show, though.
Any idea on how to fix it? Or a different approach?
edit: I need the Item because I have some properties in it.

As #folibis mentioned, you can consider using TableView or ListView since they have built-in support for header, scrolling, and delegates for your model data.
I created an example that demonstrates:
ListView
Horizontal scrolling with orientation: ListView.Horizontal
Scroll bar with ScrollBar.vertical: ScrollBar { }
Header by setting header and headerPositioning
Content provided by setting model and delegate
import QtQuick
import QtQuick.Controls
Page {
ListView {
anchors.verticalCenter: parent.verticalCenter
width: parent.width
height: 120
model: [ "Alligator", "Bear", "Cat", "Dog",
"Elephant", "Flamingo", "Giraffe", "Horse",
"Iguana", "Jellyfish", "Kangaroo", "Lion",
"Monkey", "Narwhal", "Owl", "Panda",
"Quail", "Raccoon", "Squirrel", "Tiger",
"Unicorn", "Vampire Bat", "Worm", "Xenarthra",
"Yak", "Zebra"
]
orientation: ListView.Horizontal
ScrollBar.horizontal: ScrollBar {
height: 20
policy: ScrollBar.AlwaysOn
}
header: Rectangle {
width: 100
height: 100
color: "white"
border.color: "grey"
z: 2
Text {
anchors.fill: parent
text: "Alphabet Zoo"
horizontalAlignment: Qt.AlignHCenter
verticalAlignment: Qt.AlignVCenter
wrapMode: Text.WordWrap
}
}
headerPositioning: ListView.OverlayHeader
clip: true
delegate: Rectangle {
width: 100
height: 100
color: index & 1 ? "#eee" : "#ddd"
border.color: "grey"
Text {
anchors.fill: parent
text: modelData
horizontalAlignment: Qt.AlignHCenter
verticalAlignment: Qt.AlignVCenter
wrapMode: Text.WordWrap
}
}
}
}
You can Try it Online!

Related

Why horizontal scroll bar not working on listview qml?

I have used almost all solutions on Stackoverflow to make it work. But none of them worked?
What changes can be done so that horizontal scroll bar work?
I used the qt docs also but still not working. Please give some examples. I just need horizontal scroll in listview. Or other view also.
main.qml
Rectangle {
id: frame
width: 300
height: 300
anchors.top: meaning.bottom
ListView {
width: 300
height: 300
anchors.centerIn: parent
id: myList
model: myModel
highlight: highlightBar
clip: true
snapMode: ListView.SnapToItem
headerPositioning: ListView.OverlayHeader
header: Rectangle {
id: headerItem
width: myList.width
height: 30
z: 2
color: "gray"
Text {
text: "Simple Text List"
color: "white"
}
}
delegate: Item {
id: delegateItem
width: 400
height: 20
Text {
text: name
}
MouseArea {
id: mArea
anchors.fill: parent
onClicked: {
myList.currentIndex = index
}
}
}
}
Component {
id: highlightBar
Rectangle {
width: parent.width
height: 20
color: "#FFFF88"
}
}
ListModel {
id: myModel
}
ScrollBar {
id: vbar
hoverEnabled: true
active: hovered || pressed
orientation: Qt.Vertical
size: frame.height / content.height
anchors.top: parent.top
anchors.right: parent.right
anchors.bottom: parent.bottom
}
ScrollBar {
id: hbar
hoverEnabled: true
active: hovered || pressed
orientation: Qt.Horizontal
size: frame.width / content.width
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
}
}
Please make change to this so horizontal scroll work.
Thanks.
QML ListView is a Flickable subclass.
To show scrollbars in a Flickable you need to provide contentWidth and/or contentHeight and the desired flickableDirection.
Here is an example of a ListView that scrolls both ways:
import QtQuick
import QtQuick.Controls
import QtQuick.Window
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListView {
anchors.fill: parent
ScrollBar.vertical: ScrollBar {}
ScrollBar.horizontal: ScrollBar {}
flickableDirection: Flickable.HorizontalAndVerticalFlick
contentWidth: 1000
model: ["lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "sed", "do", "eiusmod", "tempor", "incididunt", "ut", "labore", "et", "dolore", "magna", "aliqua", "ut", "enim", "ad", "minim", "veniam", "quis", "nostrud", "exercitation", "ullamco", "laboris", "nisi", "ut", "aliquip", "ex", "ea", "commodo", "consequat", "duis", "aute", "irure", "dolor", "in", "reprehenderit", "in", "voluptate", "velit", "esse", "cillum", "dolore", "eu", "fugiat", "nulla", "pariatur", "excepteur", "sint", "occaecat", "cupidatat", "non", "proident", "sunt", "in", "culpa", "qui", "officia", "deserunt", "mollit", "anim", "id", "est", "laborum"]
delegate: Label {
width: ListView.view.width
horizontalAlignment: Text.AlignHCenter
text: modelData
}
}
}

How to center elements in ColumnLayout

how can I center elements in ColumnLayout?
Here is my qml code:
ApplicationWindow {
id: root
visible: true
width: 640
height: 640
title: qsTr("Your Booking")
GridLayout{
anchors.fill: parent
columns: 2
flow: GridLayout.TopToBottom
Rectangle{
id: appBar
Layout.columnSpan: 2
width: root.width
height: root.height/10
color: "red"
}
ColumnLayout{
spacing:5
id: columnData
height: root.height - appBar.height
width: root.width/2
ComboBox{
anchors.horizontalCenter: parent.horizontalCenter
}
ComboBox{
}
ComboBox{
}
ComboBox{
}
ComboBox{
}
}
ColumnLayout{
}
}
}
I want to center ComboBoxes in ColumnLayout.
You should avoid using anchors and layouts at the same time. Mixing them at the same level would lead to the malfunction of the layouts or some unexpected results (however, using anchors inside the items in layouts is ok).
To align items in layouts, you can use the attached properties: Layout.alignment, e.g.:
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
This statement would make your item stay exactly in the center of your layout.

Editing a TextInput in a ScrollView

I have an issue with my QML. I'd like to edit a TextInput based on an action, setting the focus attribute to true. It works when the TextInput is located in a Rectangle, but not in a ScrollView.
Here is an example:
Item {
id: main
width: 640
height: 480
ScrollView{
id: scrollView
height: parent.height/2
width: parent.width
Rectangle{
border.color: "black"
border.width: 1
anchors.centerIn: parent
height: 25
width: 200
TextInput{
id: ti1
anchors.fill: parent
verticalAlignment: TextInput.AlignVCenter
horizontalAlignment: TextInput.AlignHCenter
}
}
}
Rectangle{
y: height
height: parent.height/2
width: parent.width
Rectangle{
border.color: "black"
border.width: 1
anchors.centerIn: parent
height: 25
width: 200
TextInput{
id: ti2
anchors.fill: parent
verticalAlignment: TextInput.AlignVCenter
horizontalAlignment: TextInput.AlignHCenter
}
}
}
MouseArea{
anchors.fill: parent
onClicked: {
if (mouseY < parent.height/2){
ti2.focus = false
ti1.focus = true
}else{
ti1.focus = false
ti2.focus = true
}
}
}
}
When I click on the bottom half of the window, the TextInput ti2 is editable. But when I click on the top half, ti1 is not.
Does anybody have any idea? The behaviour is the same with TextEdit.
Thanks.
I think it is because:
"Only one Item can be a direct child of the ScrollView and the child is implicitly anchored to fill the scroll view.".
From: http://doc.qt.io/qt-5/qml-qtquick-controls-scrollview.html
Perhaps the tree of components is unavailable in a ScrollView.
But if you use:
ti1.forceActiveFocus();
instead of:
ti1.focus = true
it works.

Need margins between elements in QML ListView

I need margins between elements in QML ListView. With this code I get margin of column of elements, but I need margin between each element:
ListModel {
id: listModel
ListElement {
name: "Apple"
}
ListElement {
name: "Banana"
}
}
Component {
id: listDelegate
Rectangle {
width: 250; height: 100
anchors.margins: 30
color: "green"
Text {
id: itexItem
anchors.leftMargin: 20
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: 40
text: name
}
}
}
ListView {
id: listView
anchors.fill: parent;
anchors.margins: 50
model: listModel
delegate: listDelegate
focus: true
}
I get margin between box of list element. Is it work fine. I need margin between each element of ListView. I need column like:
Element "Apple"
margin
Element "Banana"
Add:
spacing: value
to your ListView properties.

Nested ScrollView in QML doesn't respond to mousewheel

I have a nested ScrollView, similar to the following QML:
import QtQuick 2.0
import QtQuick.Controls 1.1
Rectangle {
width: 200
height: 600
ScrollView {
id: sView
anchors.fill: parent
ListView {
id: list
boundsBehavior: Flickable.StopAtBounds
clip: true
focus: true
interactive: true
model: 5
delegate: Component {
MouseArea {
id: hoverArea
width: 100
height: 200
onClicked: list.currentIndex = index;
Rectangle {
id: fauxParent
anchors.fill: parent
border.width: 1
border.color: "black"
Rectangle {
anchors.top: parent.top
anchors.left: parent.left
height: parent.height
width: parent.width / 2
border.width: 1
border.color: "purple"
color: "green"
Text {
anchors.centerIn: parent
text: "stuff"
}
}
ScrollView {
//parent: sView
anchors.top: fauxParent.top
anchors.right: fauxParent.right
height: fauxParent.height
width: fauxParent.width / 2
ListView {
model: 3
delegate: Component {
Rectangle {
radius: 10
height: 100
width: 100
color: "blue"
}
}
}
}
}
}
}
}
}
}
It seems to run correctly, except that the inner ScrollView won't respond to the mousewheel: the outer ScrollView intercepts that event. The only fix I've found in research for this, is to set the inner scrollview's parent directly to the outer scrollview (uncomment the parent: sView line). Unfortunately, this re-positions all five scrollview delegates onto the top right corner of the outer scrollview. It seems that ScrollView positions itself based on its parent?
For the record, my actual application is wrapping a large section of the page in a scrollview so as to allow the user to access sections of it that may be out of bounds for the current window size. The content of this section, though, has a variety of different controls for a variety of different purposes, including some scrollviews. So I'd also accept an alternate way of moving around a set of generic content that's too large for the window.
This is a Windows desktop app, so I don't need to consider mobile-specific issues.
You nested four elements that handle scroll Events.
Why do you put a ScrollView arround a ListView?
If you remove the ScrollViews the Mousewheel work fine.
Rectangle {
width: 200
height: 600
ListView {
anchors.fill: parent
id: list
boundsBehavior: Flickable.StopAtBounds
clip: true
focus: true
interactive: true
model: 5
delegate: Component {
MouseArea {
id: hoverArea
width: 100
height: 200
onClicked: list.currentIndex = index;
Rectangle {
id: fauxParent
anchors.fill: parent
border.width: 1
border.color: "black"
Rectangle {
anchors.top: parent.top
anchors.left: parent.left
height: parent.height
width: parent.width / 2
border.width: 1
border.color: "purple"
color: "green"
Text {
anchors.centerIn: parent
text: "stuff"
}
}
ListView {
anchors.top: fauxParent.top
anchors.right: fauxParent.right
height: fauxParent.height
width: fauxParent.width / 2
model: 3
delegate: Component {
Rectangle {
radius: 10
height: 100
width: 100
color: "blue"
}
}
}
}
}
}
}
}
If you miss the Scrollbar look at this:
How to create scrollbar in QtQuick 2.0?

Resources