QML ScrollView with ColumnLayout - c++

I am trying to create a scroll view around a ColumnLayout, unfortunately my current code doesn't work. I know about ListView, but in my case I need to create scrollable Layout, because it will contain heterogeneous elements.
ApplicationWindow {
id: mainwindow
title: qsTr("Hello World")
width: 300
height: 300
visible: true
ScrollView {
anchors.fill: parent
ColumnLayout {
width: mainwindow.width
Image {
anchors.bottomMargin: 10
source: "img/button.jpg"
width: parent.width
height: 400
}
Image {
source: "img/button.jpg"
width: parent.width
height: 500
}
}
}
}
This renders to this (which is clearly not what I want):
There are two problems:
Images are not stretched across the entire window width, parent.width is ignored. I want images to have exact same width as ScrollView (no horizontal scroll)
Image height property is ignored
What am I doing wrong?

I would go with a plain column and access the desired width property directly by id. As I understand these container elements are measuring their size depending on their content, that might be the reason why setting the ColumnLayouts width has no effect.
This works for me:
ScrollView
{
anchors.fill: parent
Column {
Repeater {
model: 4;
delegate: Item {
width: root.width;
height: image.sourceSize.height;
Image {
id: image;
anchors.centerIn: parent;
width: parent.width;
fillMode: Image.Stretch;
source: "img" + (index+1) + ".png"
}
}
}
}
}
In my case root is just the parent's id. Hope this helps!

Same problem on my side. This worked for me :
ScrollView {
width: parent.width
height : parent.height
contentWidth: column.width // The important part
contentHeight: column.height // Same
clip : true // Prevent drawing column outside the scrollview borders
Column {
id: column
width: parent.width
// Your items here
}
}

Related

How to Centered the Rectangle in qml Scroll View

I had created a video player in QML having zooming functionality. I want to add the Google Map like zooming functionality in it that is zoom from the center of the current visible frame. I can center the image using:
videoScrollControl1.flickableItem.contentX = (videoScrollControl1.flickableItem.contentWidth-imgVideo.width)/2
videoScrollControl1.flickableItem.contentY= (videoScrollControl1.flickableItem.contentHeight-imgVideo.height)/2
but the problem is that it always zoom to the center of the Image. I want to zoom from any point or from the center of current visible frame. my Code is as follows:
ScrollView {
id: videoScrollControl1
anchors.fill: parent
anchors.margins: 5
frameVisible: false
contentItem: Rectangle {
id: videoAreaMain1
width: 620
height: 470
color: "#000000"
Image {
id: imgVideo
width: parent.width
height: parent.height
anchors.left: parent.left
anchors.top: parent.top
fillMode: Image.PreserveAspectFit
MouseArea {
id: zooingWheeler
anchors.fill: parent
hoverEnabled: true
}
}
}
}
Thanks in advance.

Hover effect is scrolling the list on a QML GridView

I have a QML GridView with a scrollbar and hover effect, when I move the curso to the page's bottom the hover make an automatic scrolling, I want to stop it. I tried to set the interactive property of Flickable to false "interactive: false" but it didn't work. How can I stop this behavior?
Obs: When I remove the hover effect the scroll behave in the expected way, just moving through the scrollbar.
GridView{
id: grid
anchors.margins: 20
anchors.fill: parent
cellHeight: 80
cellWidth: 80
model: MyModel{}
highlight: Rectangle {
color: "lightsteelblue"
height: parent.cellHeight
width: parent.cellWidth
z:2
opacity: 0.7
}
delegate: Column {
Rectangle {
color: myColor;
height: grid.cellHeight * 0.7
width: grid.cellWidth * 0.7
border.color: "white"
anchors.margins: 5
anchors.horizontalCenter: parent.horizontalCenter
MouseArea {
id: mouseRegion
anchors.fill: parent
hoverEnabled: true
onEntered: grid.currentIndex = model.index
}
}
Text {
text: name;
anchors.horizontalCenter: parent.horizontalCenter }
}
}
Here is the Link to a simple project that reproduces the behavior: https://drive.google.com/open?id=0B7WUSCDDdwtIbWVDWVFvMjM1djA
My workmate help me to figured out a solution to my problem. I was using currentIndex property to set the hover position in the gridview, as described on documentation this property will smoothly scroll the GridView in the way that the current item becomes visible if the highlightFollowsCurrentItemis set to true (default value) and will not scroll if highlightFollowsCurrentItem set to false.
However, after set highlightFollowsCurrentItem property to false, the automatic scroll stopped together with my hover effect, which is clearly unwanted. I couldn't figure out what was wrong and we came out with a different approach.
To make the hover work without the automatic scrolling provided by currentIndex behavior, we remove it from gridview and used onEntered and onExited to control hover behavior, changing the rectangle visibility (id:selectedItem) used to simulate the hover effect as showed below.
GridView{
id: grid
anchors.margins: 20
anchors.fill: parent
cellHeight: 80
cellWidth: 80
model: MyModel{}
delegate: Item{
height: grid.cellHeight * 0.9
width: grid.cellWidth * 0.7
Rectangle {
id: selectedItem
color: "lightsteelblue"
height: parent.height
width: parent.width
z:12
opacity: 0.7
visible: false
}
Rectangle {
id:rect
color: myColor;
height: parent.height-textName.height
width: parent.width
border.color: "white"
anchors.margins: 5
anchors.horizontalCenter: parent.horizontalCenter
MouseArea {
id: mouseRegion
anchors.fill: parent
hoverEnabled: true
onEntered: selectedItem.visible = true
onExited: selectedItem.visible = false
}
}
Text {
id: textName
text: name;
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom
}
}
}
It allowed me to avoid automatic scroll and keep the hover effect work properly.

How to manage QML ScrollView navigation with respect to child QML TextEdit?

I have a TextEdit inside a Scrollview inside a SplitView. I have a Q_INVOKABLE function that I call when a row in a TableView gets selected to jump to a desired line in the TextEdit, this works fine. However, I need to adjust the ScrollView focus so that it moves when the selection of the TextEdit moves.
Identical behavior to selecting a compiling error on an IDE.
//main.qml
ScrollView {
id: palGenTextScrollView
anchors.fill: parent
TextEdit {
id: mainTextEdit
text: fileio.palFileText
wrapMode: TextEdit.Wrap
selectByMouse: true
}
TableView {
id: errorsTableView
onClicked: {
mainTextEdit.select(palerrorviewmodel.goToLine(errorsTableView.currentRow),
palerrorviewmodel.goToLine(errorsTableView.currentRow))
mainTextEdit.forceActiveFocus()
//Call something to adjust ScrollView here
//palGenTextScrollView. ??
}
I omitted some irrelevant code.
You need to use palGenTextScrollView.flickableItem.contentY to set the position of your text. Here after is a small example working: you have a button for each line of the text, and clicking on it selects the line and centers the text on it. You can work on it for your own issue.
I couldn't manage to have your example working because the palerrorviewmodel element is missing.
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ScrollView {
id: palGenTextScrollView
width: 200
height: 100
TextEdit {
id: mainTextEdit
text: "I have a TextEdit\ninside a Scrollview\ninside a SplitView.\nI have a Q_INVOKABLE\nfunction that I call\nwhen a row in a TableView\ngets selected to jump\nto a desired line\nin the TextEdit,\nthis works fine.\nHowever, I need to adjust\nthe ScrollView focus\nso that it moves\nwhen the selection\nof the TextEdit moves.\nIdentical behavior\nto selecting a compiling\nerror on an IDE."
wrapMode: TextEdit.Wrap
selectByMouse: true
}
}
Row{
spacing: 5
anchors.top: palGenTextScrollView.bottom
anchors.topMargin: 20
Repeater{
model: mainTextEdit.lineCount
delegate: Rectangle{
width: 20
height: 20
color: "blue"
Text{
anchors.centerIn: parent
text: index
}
MouseArea{
anchors.fill: parent
onClicked: {
var lines = mainTextEdit.text.split("\n");
var count=0;
for (var i=0; i<index;i++){
count+=(lines[i].length+1);
}
mainTextEdit.select(count, count+lines[index].length);
mainTextEdit.forceActiveFocus()
var maxY = mainTextEdit.contentHeight-palGenTextScrollView.height
var lineHeight = mainTextEdit.contentHeight/mainTextEdit.lineCount
var centeredY=index*lineHeight-palGenTextScrollView.height/2
if (centeredY < 0){
palGenTextScrollView.flickableItem.contentY=0
}else if (centeredY<=maxY){
palGenTextScrollView.flickableItem.contentY=centeredY
}else{
palGenTextScrollView.flickableItem.contentY=maxY
}
}
}
}
}
}
}

Qml Grid, Column, and RowLayout does not work with LayoutMirroring

I am writing a qml application that should support RTL and LTR languages and the interface needs to be some how flexible, and anchors may not produce good UI
So I planned to use qml Grid, Column, and RowLayout, they work good but does not get mirrored when I use
LayoutMirroring.enabled: true
LayoutMirroring.childrenInherit: true
is there any way to use these layout components with LayoutMirroring.enabled: true
and if not how to set width and height for qml positioners (Row,Column,and Grid) to fill thier bounding item width and height
LayoutMirroring is not available for RowLayout, ColumnLayout or GridLayout. You can use Row[View], Column[View] or Grid[View] instead. See http://qt-project.org/doc/qt-5.1/qtquick/qml-qtquick2-layoutmirroring.html for more information.
Here is a quick example for qml positioners:
Rectangle {
width: 640
height: 480
color: "#303030"
Rectangle {
width: parent.width / 1.1
height: parent.height / 1.1
anchors.centerIn: parent
color: "white"
Grid {
id: grid
anchors.fill: parent
columns: 2
spacing: 6
columnSpacing: spacing
rowSpacing: spacing
property int rowCount: Math.ceil(visibleChildren.length / columns)
property real cellWidth: (width - (columns - 1) * columnSpacing) / columns
property real cellHeight: (height - (rowCount - 1) * rowSpacing) / rowCount
Rectangle {
color: "#aa6666"
width: grid.cellWidth
height: grid.cellHeight
}
Rectangle {
color: "#aaaa66"
width: grid.cellWidth
height: grid.cellHeight
}
Rectangle {
color: "#9999aa"
width: grid.cellWidth
height: grid.cellHeight
}
Rectangle {
color: "#6666aa"
width: grid.cellWidth
height: grid.cellHeight
}
}
}
}
Change the number of columns and add or remove some rectangles, to see that it works.
in Qt 5.2 RowLayout, ColumnLayout and GridLayout have layoutDirection property to support RTL layout

grid of MouseArea in Qt QML

I have made an Item in QT QML which contains a MouseArea element.
Here is the code,
import QtQuick 1.0
Rectangle {
id: base
width: 240
height: 320
x:0; y:0
color: "#323138"
/////////////////////// MAIN FOCUSSCOPE //////////////////////
FocusScope {
id: mainfocus
height: base.height; width: base.width
focus: true
/////////////////////// MAIN GRID ///////////////////////////
GridView {
id: maingrid
width: base.width-10; height: base.height-titlebar.height-10
x: 5; y: titlebar.height+5;
cellHeight: maingrid.height/3; cellWidth: maingrid.width/3-1
Component {
id: myicon
Rectangle {
id: wrapper
height: maingrid.cellHeight-10; width: maingrid.cellWidth-10
radius: 8; smooth: true
color: GridView.isCurrentItem ? "#c0d0c0" : "transparent"
focus: true
MouseArea {
id: clickable
anchors.fill: wrapper
hoverEnabled: true
//onClicked: func()
}
Image {
id: iconpic
source: "./ui6.svg"
anchors.centerIn: wrapper
}
Text {
id: iconname
color: wrapper.GridView.isCurrentItem ? "black" : "#c8dbc8"
anchors.top: iconpic.bottom; anchors.horizontalCenter: iconpic.horizontalCenter
text: name
}
}
}
model: 4
delegate: myicon
focus: true
}
}
//////////////////////// TITLEBAR ///////////////////////
Rectangle {
id: titlebar
x:base.x
y:base.y
height: 25; width: base.width
color : "#356f47"
Text {
color: "#fdfdfd"
anchors.centerIn: titlebar
text: "title"
}
}
}
I want to make a grid of such Items so that it gives me a grid of custom made click-able Items that I created which I can use for different functions.
Using the GridView element, I was able to make such a grid which used number of my custom made Items as a template.
The problem is that when I click anyone of these Items it executes a single function since there was only one MouseArea element in my Item. I am able to detect a click on an Item, but not able to uniquely determine which Item was clicked. How do I achieve this ?
Of course, I might be doing it wrong, so other suggestions are also welcome.
Thanks
When the GridView item creates the instances they inherit the index variable. This identifies the unique item.
MouseArea {
id: clickable
anchors.fill: wrapper
hoverEnabled: true
onClicked: {
maingrid.currentIndex=index;
switch (index)
{
case 0:
//Function or method to call when clicked on first item
break;
case 1:
//Function or method to call when clicked on second item
break;
default:
//Function or method to call when clicked on another item
break;
}
}
}
I hope it helps you!