QML - GridLayout - unable to center a page using it - qt

I'm new to Qt and QML and while I'm trying to learn them I've encountered the following problem which I'm not sure why they appeared.
I'm having a button, at the moment when I'm pressing it will display a new purple Rectangle over the screen. I am trying to position it at the center of the screen, or in other words to fill the whole screen, but for some reason, I can't do that, and It's always going to start from the beginning of Item 2 instead of the beginning of the screen. Also, it's going to be placed under Item 1 and Item 3 instead to overlay them.
MY CODE:
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 2.12
Window {
id: mainD
visible: true
width: 640
height: 480
title: qsTr("Hello World")
GridLayout {
id : grid
anchors.fill: parent
rows : 12
columns : 20
property double colMulti : grid.width / grid.columns
property double rowMulti : grid.height / grid.rows
function prefWidth(item){
return colMulti * item.Layout.columnSpan
}
function prefHeight(item){
return rowMulti * item.Layout.rowSpan
}
Rectangle {
id: id1
color : 'red'
Layout.column: 0
Layout.rowSpan: 10
Layout.columnSpan: 2
Layout.preferredWidth : grid.prefWidth(this)
Layout.preferredHeight : grid.prefHeight(this)
}
Rectangle {
id: id2
color : 'green'
Layout.column: 2
Layout.rowSpan: 10
Layout.columnSpan: 18
Layout.preferredWidth : grid.prefWidth(this)
Layout.preferredHeight : grid.prefHeight(this)
Button{
width: 100
height: 100
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
onClicked: {
id2.state = "STATE_1"
}
}
StackView{
id: st
width: parent.width
height: parent.height + 100
anchors.horizontalCenter: grid.parent.horizontalCenter
anchors.verticalCenter: grid.parent.verticalCenter
visible: false
Rectangle{
anchors.fill: parent
color: "purple"
opacity: 0.7
Button{
width: 100
height: 100
onClicked: {
id2.state = "STATE_2"
}
}
}
}
states: [
State{
name: "STATE_1"
PropertyChanges {
target: st;
visible: true
}
},
State{
name: "STATE_2"
PropertyChanges {
target: st;
visible: false
}
}
]
}
Rectangle {
id: id3
color : 'blue'
Layout.column: 18
Layout.rowSpan: 10
Layout.columnSpan: 2
Layout.preferredWidth : grid.prefWidth(this)
Layout.preferredHeight : grid.prefHeight(this)
}
}
}
Can anyone explain to me what I am doing wrong?

Purple rectangle is an initial item of StackView object with an id "st". It is hidden initially.
"id2" is a visual parent of "st". Coordinates of "st" are relative to its visual parent. You are setting its width to be its parent's width and height to be parent's height + 100 so its (0,0) coordinate goes to parent's (0,0). You should read more from the documentation: Visual Parent in Qt Quick.
If you want to fill the whole screen with purple rectangle and position it on top of other visual items you can do it by moving "st" out from the "grid" and place it on the same level with it. Now both "grid" and "st" are children of "mainD", and because StackView is below "grid" you don't need to play with z values but when "st" becomes visible it is rendered on top of "grid".
Window {
id: mainD
...
GridLayout {
id : grid
...
}
StackView{
id: st
width: parent.width
height: parent.height// + 100
//anchors.horizontalCenter: grid.parent.horizontalCenter
//anchors.verticalCenter: grid.parent.verticalCenter
visible: false
Rectangle{
anchors.fill: parent
color: "purple"
opacity: 0.7
Button{
width: 100
height: 100
onClicked: {
id2.state = "STATE_2"
}
}
}
}
}
I recommend Positioning elements chapter from free Qt5 Cadaques book for further reading.

I'm having a button, at the moment when I'm pressing it will display a new purple Rectangle over the screen. I am trying to position it at the center of the screen, or in other words to fill the whole screen
As #talamaki mentioned if you want the purple to fill the whole background it is better to have it as child of the main Item instead of having it be part of the grid, and then have it extend the allocated space inside the grid.
but for some reason, I can't do that, and It's always going to start
from the beginning of Item 2 instead of the beginning of the screen.
That is because of how grids work. Think of it as like a chess board, item 1 already occupies the first two columns so item 2 will start from 3rd column, and any child of item 2 stars from the coordinate system of item 2. You could make it extend beyond it's starting point but I think it would be cleaner to have the background on the root item.
Also, it's going to be placed under Item 1 and Item 3 instead to
overlay them.
To have the purple be underneath 1 and 3 it has to be drawn first (should come first in the QML). If you don't want the button to be affected you should put it below the GridLayout in the QML file so that it is drawn last over it. Putting all this together would be something like this:
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 2.12
Window {
id: mainD
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle{
id: background
anchors.fill: parent
color: "purple"
opacity: 0.7
visible: false
}
GridLayout {
id : grid
anchors.fill: parent
rows : 12
columns : 20
property double colMulti : grid.width / grid.columns
property double rowMulti : grid.height / grid.rows
function prefWidth(item){
return colMulti * item.Layout.columnSpan
}
function prefHeight(item){
return rowMulti * item.Layout.rowSpan
}
Rectangle {
id: id1
color : 'red'
Layout.column: 0
Layout.rowSpan: 10
Layout.columnSpan: 2
Layout.preferredWidth : grid.prefWidth(this)
Layout.preferredHeight : grid.prefHeight(this)
}
Rectangle {
id: id2
color : 'green'
Layout.column: 2
Layout.rowSpan: 10
Layout.columnSpan: 16
Layout.preferredWidth : grid.prefWidth(this)
Layout.preferredHeight : grid.prefHeight(this)
}
Rectangle {
id: id3
color : 'blue'
Layout.column: 18
Layout.rowSpan: 10
Layout.columnSpan: 2
Layout.preferredWidth : grid.prefWidth(this)
Layout.preferredHeight : grid.prefHeight(this)
}
}
Button{
width: 100
height: 100
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
onClicked: {
background.visible = !background.visible
}
}
}
From your question it is not clear to me if you want Item 2 to disappear, have its colour blended with background, or have it remain on top when you click the button.

Related

Qt Qml Vertical Tabbar

I would like to add Vertical TabBar to my app in a similar manner of what Qt Creater is doing in their app (as shown in picture).
I have been searching how to simple make the TabBar vertical, yet did not find proper answers (thought its common to have it vertical).
Question: How could I make a Vertical Tab to navigate through the different qml files I have? If there are more suitable options, please suggest.
A TabBar just uses a common ListView to display a bunch of TabButtons. You can customize it by overwriting the contentItem property and making the ListView vertical, like this:
// VertTabBar.qml
TabBar {
id: control
contentItem: ListView {
model: control.contentModel
currentIndex: control.currentIndex
spacing: control.spacing
orientation: ListView.Vertical // <<-- VERTICAL
boundsBehavior: Flickable.StopAtBounds
flickableDirection: Flickable.AutoFlickIfNeeded
snapMode: ListView.SnapToItem
highlightMoveDuration: 0
highlightRangeMode: ListView.ApplyRange
preferredHighlightBegin: 40
preferredHighlightEnd: height - 40
}
}
A complete example using Fusion theme.
It is important to set the width of the TabButton else the width is divided by the number of items.
Notice there is a light colour separator line, that comes from ???
Issue: first item can be partially clipped.
Well, there are a lot of things under the hood with QML...
Thus, we cannot really make a TabBar vertical...
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Templates as T
// import QtQuick.Controls.impl
// import QtQuick.Controls.Fusion
// import QtQuick.Controls.Fusion.impl
Page {
id: root
width: 1800
height: 800
Row {
anchors.fill: parent
TabBar {
id: control
width: 200
height: parent.height
contentItem: ListView {
model: control.contentModel
currentIndex: control.currentIndex
spacing: control.spacing
orientation: ListView.Vertical
boundsBehavior: Flickable.StopAtBounds
flickableDirection: Flickable.AutoFlickIfNeeded
snapMode: ListView.SnapToItem
highlightMoveDuration: 0
highlightRangeMode: ListView.ApplyRange
preferredHighlightBegin: 40
preferredHighlightEnd: width - 40
}
Repeater {
model: 50
TabButton {
id: control2
width: control.width
text: "tab blabla blabla %1".arg(model.index)
contentItem: IconLabel {
spacing: control2.spacing
mirrored: control2.mirrored
display: control2.display
icon: control2.icon
text: control2.text
font: control2.font
color: control2.palette.buttonText
}
background: Rectangle {
y: control2.checked || control2.TabBar.position !== T.TabBar.Header ? 0 : 2
implicitHeight: 21
height: control2.height - (control2.checked ? 0 : 2)
border.color: Qt.lighter(Fusion.outline(control2.palette), 1.1)
border.width: 0
gradient: Gradient {
GradientStop {
position: 0
color: control2.checked ? Qt.lighter(Fusion.tabFrameColor(control2.palette), 1.04)
: Qt.darker(Fusion.tabFrameColor(control2.palette), 1.08)
}
GradientStop {
position: control2.checked ? 0 : 0.85
color: control2.checked ? Qt.lighter(Fusion.tabFrameColor(control2.palette), 1.04)
: Qt.darker(Fusion.tabFrameColor(control2.palette), 1.08)
}
GradientStop {
position: 1
color: control2.checked ? Fusion.tabFrameColor(control2.palette)
: Qt.darker(Fusion.tabFrameColor(control2.palette), 1.16)
}
}
}
}
}
}
StackLayout {
id: stack_layout
width: parent.width - 200
height: parent.height
currentIndex: control.currentIndex
Repeater {
model: 50
Item {
Label {
anchors.centerIn: parent
text: "tab %1".arg(model.index)
font.pixelSize: 50
}
}
}
}
}
}

Purpose of z property with real value in QML ListView

In the below sample code, I set z property for the highlight item and depending on the value, it shows up to the user. The z property can also be configured with real value.
It means z value can have fractional values such as 0.1 or 1.2, like that.
Can anyone explain the purpose of z value should be fractional or real value in QML ListView?
import QtQuick 2.8
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Sample List View")
property var delHeight: 50
ListView {
id: listView
anchors.fill: parent
cacheBuffer: 100
footer: Rectangle {
width: (listView.orientation === ListView.Horizontal) ? 200 : parent.width
height: delHeight
color: "lightyellow"
Text {
anchors.centerIn: parent
font.pointSize: 20
text: "Footer"
}
}
header: Rectangle {
width: (listView.orientation === ListView.Horizontal) ? 200 : parent.width
height: delHeight
color: "lightblue"
Text {
anchors.centerIn: parent
font.pointSize: 20
text: "Header"
}
z: 2
}
headerPositioning: ListView.OverlayHeader
highlight: Rectangle {
color: "white"
opacity: 0.5
border.color: "blue"
border.width: 5
z: 1.2
Component.onCompleted: {
console.log ("Created hightlight component with z factor: " + z)
}
}
// highlightMoveDuration: 10000
// highlightRangeMode: ListView.ApplyRange
keyNavigationEnabled: true
model: 20
delegate: componentId
layoutDirection: Qt.RightToLeft
orientation: ListView.Vertical
snapMode: ListView.SnapOneItem
Component.onCompleted: {
currentIndex = 5
}
focus: true
onFocusChanged: {
console.log ("Focus: " + focus)
}
}
Component {
id: componentId
Rectangle {
width: (listView.orientation === ListView.Horizontal) ? 200 : parent.width
height: delHeight
color: "lightgreen"
border.color: "black"
border.width: (listView.currentIndex === index) ? 5 : 1
Text {
anchors.centerIn: parent
font.pointSize: 20
text: "Element: " + index
}
MouseArea {
anchors.fill: parent
onClicked: {
listView.currentIndex = index
}
}
Component.onCompleted: {
console.log ("Created component: " + index)
}
Component.onDestruction: {
console.log ("Destroyed component: " + index)
}
}
}
}
The z property of an item gives the stacking order to that item.
Meaning, if you are constructing two Rectangles one after the other than recently constructed rectangle will be stacked on top the previously constructed one.
Ex code:
Item {
Rectangle {
color: "red"
width: 100; height: 100
}
Rectangle {
color: "blue"
x: 50; y: 50; width: 100; height: 100
}
}
Here red rectangle stacked below blue rectangle.
Now QML gives you chance to change the stacking order through z property of the item.
in the above example if I assign z property of the red rectangle to have a value of anything above 0, I would see it on top of blue rectangle. So z property has changed the stacking order for the sibling item.
The purpose of the z property stays the same in case of ListView highlight. When you want to see the highlight item then you have to give it a value which is greater than the items which will be constructed. You can check this by just setting z property for the componentIds rectangle to some higher value than highlights z value.
NOTE: it only works for sibling items.
More explanation can be found here
Read about the real type here

z ordering of elements qt qml

I'm struggling with an issue and I can't find a solution.
I am developing an embedded device (a graphic interface for an oven) with Qt.
I have the main page where I have a SwipeView with a grid inside to show n-tiles.
The tile is defined in another object.qml that I call in the main page and on each tile I have an image with 3 dots and when you click on it, a popup comes out that lets you edit the tile.
The problem is showing this popup because when I click on the three-dot-image the popup object shows underneath the tile and I can't seem to solve this problem.
I tried changing the z property but it doesn't work.
Anyway, I'm gonna attach some code and two images of the interface.
Thank you
MyPgRecipeGrid.qml this is my main page
import QtQuick 2.6
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.3
SwipeView {
id: view
property int numProgrammi : myVar.progCategoryRecipeGrid.count
currentIndex: 0
width:parent.width
height: parent.height*0.75
anchors.top: searchRect.bottom; anchors.topMargin: parent.height*0.025
Repeater {
id: gridRepeat
property int numgrid: ((Math.floor(view.numProgrammi/12)) + (((view.numProgrammi%12)==0) ? 0 : 1))
model: numgrid
delegate: Rectangle {
color: "transparent"
GridView {
id:grid
width: parent.width*0.95; height: parent.height
anchors.horizontalCenter: parent.horizontalCenter
clip: false
property int numPage: index
cellWidth: 190; cellHeight: 180
interactive: false
model: 12 //Draws 12 tiles
delegate: Rectangle {
width: grid.cellWidth; height: grid.cellHeight
color: "transparent"
TileCategoryRecipeGrid {
property int indicelista: ((grid.numPage * 12)+index < myVar.progCategoryRecipeGrid.count) ? ((grid.numPage * 12 )+index) : 0
visible: ((grid.numPage*12)+index) < view.numProgrammi ? true : false
nomeTypCat: qsTr(myVar.progCategoryRecipeGrid.get(indexlist).nameCategory)
urlimageTypCat: myVar.progCategoryRecipeGrid.get(indexlist).urlCategoryImage
emptyTypCat: myVar.progCategoryRecipeGrid.get(indexlist).emptyCategory
userTypCat: myVar.progCategoryRecipeGrid.get(indexlist).userCategory
}
}
}
}
}
}
TileCategoryRecipeGrid.qml this is where I build the tile
import QtQuick 2.6
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.3
//Tile
Button{
id: tileCategoryRecipeGrid
width: 180; height: 172
property string myFont: myVar.medium
property string myFont2: myVar.fontTile
background: Rectangle {
anchors.fill: parent;
color: "transparent"; radius: 2
opacity: parent.down ? 0.80 : 1
clip: true
Image {
anchors.fill: parent
anchors.horizontalCenter: parent.horizontalCenter;
anchors.verticalCenter: parent.verticalCenter; anchors.verticalCenterOffset: -10
source: image
}
}
}
Button{
id: btnPoints
width: 35; height: 35
anchors.right: parent.right; anchors.rightMargin: 3
anchors.top: parent.top; anchors.topMargin: 3
background: Rectangle {
id: threePoints
anchors.fill: parent;
color: "transparent";
opacity: parent.down ? 0.25 : 1
Image {
anchors.fill: parent
source: contextMenu.visible ? "qrc:/QmlContents/IMG/close_btn.png" : "qrc:/QmlContents/IMG/threepoints.png"
}
}
onClicked: {
contextMenu.visible == false ? contextMenu.visible = true : contextMenu.visible = false
indexLocationPopup = index
}
}
Text {
id: showCookingTime
anchors.left: parent.left; anchors.leftMargin: 42
anchors.top: parent.top; anchors.topMargin: 3
text: qsTr("00:20"); color: clrPaletta.white
font.family: myFont; font.pixelSize: 20
}
contentItem: Rectangle{
anchors.fill: parent; opacity: parent.down ? 0.80 : 1
color: "transparent"
Text{
color: clrPaletta.white; opacity: 0.50
text: qsTr("cooking type")
font.family: myFont ; font.pixelSize: 17
anchors.left: parent.left ; anchors.leftMargin: parent.width*0.05
anchors.bottom: parent.bottom; anchors.bottomMargin: parent.height*0.10
}
//Popup edit tile
ContextMenuEditTile {
id: contextMenu
visible: false
x: {
switch(indexLocationPopup) {
case 0: dp(parent.width*0.60); break
case 1: -dp(parent.width-parent.width*0.70); break
case 2: -dp(parent.width-parent.width*0.70); break
case 3: dp(parent.width*0.60); break
case 4: -dp(parent.width-parent.width*0.70); break
case 5: -dp(parent.width-parent.width*0.70); break
case 6: dp(parent.width*0.60); break
case 7: -dp(parent.width-parent.width*0.70); break
case 8: -dp(parent.width-parent.width*0.70); break
case 9: dp(parent.width*0.60); break
case 10: -dp(parent.width-parent.width*0.70); break
case 11: -dp(parent.width-parent.width*0.70); break
}
}
y: {
switch(indexLocationPopup) {
case 0: dp(parent.height-parent.height*0.75); break
case 1: dp(parent.height-parent.height*0.75); break
case 2: dp(parent.height-parent.height*0.75); break
case 3: dp(parent.height-parent.height*0.75); break
case 4: dp(parent.height-parent.height*0.75); break
case 5: dp(parent.height-parent.height*0.75); break
case 6: dp(parent.height-parent.height*0.75); break
case 7: dp(parent.height-parent.height*0.75); break
case 8: dp(parent.height-parent.height*0.75); break
case 9: -dp(parent.height+parent.height*0.30); break
case 10: -dp(parent.height+parent.height*0.30); break
case 11: -dp(parent.height+parent.height*0.30); break
}
}
z: ((indexLocationPopup >= 0) && (indexLocationPopup <= 11)) ? 99 : 0
}
}
}
ContextMenuEditTile.qml and this is my popup
import QtQuick 2.7
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.3
Rectangle {
id:contextMenu
width: 245; height: 265
visible: false
radius: 2;
color: clrPaletta.tileMenuclr1
ListView {
id:listView
anchors.fill: parent; clip: true;
boundsBehavior: Flickable.StopAtBounds
model: ListModel{
id: model
ListElement{ name:qsTr("Accessories"); urlImage: "qrc:/QmlContents/IMG/accessories.png" }
ListElement{ name:qsTr("Copy"); urlImage: "qrc:/QmlContents/IMG/copy.png" }
ListElement{ name:qsTr("Rename"); urlImage: "qrc:/QmlContents/IMG/rename_folder.png" }
ListElement{ name:qsTr("Modify"); urlImage: "qrc:/QmlContents/IMG/move_icon.png" }
ListElement{ name:qsTr("Delete"); urlImage: "qrc:/QmlContents/IMG/delete_folder.png" }
}
delegate: Button{
id:buttonLista
width: parent.width; height: listView.height/5
contentItem: Rectangle {
anchors.fill: parent; color: "transparent"
opacity: this.down ? 0.80 : 1
Rectangle{
width: parent.width; height: 1;
color: clrPaletta.lineTileContxMenu
anchors.bottom: parent.bottom;
visible: model.index < 4 ? true : false
}
Text {
id:testoItem
text: qsTr(name)
font.capitalization: Font.Capitalize; font.family: myVar.fontTile
color: clrPaletta.black; font.pixelSize: 18
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left; anchors.leftMargin: 65
}
Image {
id:imageList
source: urlImage
anchors.left: parent.left; anchors.leftMargin: 20
anchors.verticalCenter: parent.verticalCenter
}
}
}
}
}
Just have a Item { id: overlay } that is last in main.qml, this way it is guaranteed to be on top of the rest of the content, and show up your popup parented to the overlay.
It will be better to have at most one of those at a time and centered in the screen for better user experience. You could however map the particular tile position to the screen to have the popup appear relative to it.
It will also be nice if the popup has an underlay that fills the "empty" area, so clicking outside of the popup closes it.
This means you won't have to bother with any manual z ordering whatsoever. Besides, it would only work for close siblings and such, good luck achieving the desired results in your use case...
Here is a quick example how you could reuse a single popup menu and have it connected to an arbitrary item to access its functionality:
Window {
visible: true
width: 600
height: 300
GridView {
id: view
model: 6
anchors.fill: parent
cellWidth: 200
cellHeight: 150
delegate: Rectangle {
id: dlg
width: 200
height: 150
color: Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
function foo() { return index }
MouseArea {
anchors.fill: parent
onClicked: menu.item = dlg // to open the menu for this item
}
}
}
Item { // the overlay
anchors.fill: parent
visible: menu.item
MouseArea {
anchors.fill: parent
onClicked: menu.item = null // close the menu
}
Rectangle {
color: "black"
anchors.fill: parent
opacity: .5
}
Rectangle {
color: "white"
anchors.fill: menu
anchors.margins: -10
}
Column {
id: menu
anchors.centerIn: parent
property Item item: null
Button {
text: "index"
onClicked: console.log(menu.item.foo())
}
Button {
text: "color"
onClicked: console.log(menu.item.color)
}
}
}
}
You could try to create your context menu dynamically with SwipeView component set as parent:
var comp = Qt.createComponent("ContextMenuEditTile.qml");
var contextMenu = comp.createObject(view);
With this solution you do not need to struggle around with z-index values. At least when you use asynchonous Loader component the z-index will not work at all.
After creating the context menu you have to set your x and y values accordingly:
contextMenu.x = (your big switch case)
contextMenu.y = (your big switch case)
contextMenu.visible = true;
Read that first : http://doc.qt.io/qt-5/qml-qtquick-item.html#z-prop
the Z property order sibling items.
The problem here is only hierarchy, try to change your root, use rect or other instead of swipeview and make your swipeview and your button its childrens.

Qt QML anchor not working in custom Item

I'm new of qml.
I'm starting to develop a little application with a custom item.
when I try to use in application anchor.top: first_item.bottom, to position the rectangles of the custom component, one below the other doesn't work.
content file main.qml:
import QtQuick 2.5
Item
{
id:main_screen
Rectangle
{
width: 300
height: 60
id: text_content
color: "DarkGray"
opacity: 0.9
border.color: "blue"
border.width: 3
radius: 5
z:6
Text {
id: titleText
width: parent.width
height: parent.height
font.pointSize: 20
font.family: "Arial"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
text: "Test - title"
color: "White"; style: Text.Raised;
}
}
//..................This rectangle is shown below main_screen... so is OK
Custom_item
{
id:first_item
anchors.top: main_screen.bottom
}
//..................This rectangle is not shown below first_item... but it shown on absolute top, in overlap of retangle title
Custom_item
{
id:second_item
anchors.top: first_item.bottom
}
//..................This rectangle is not shown below second_item... but it shown on absolute top, in overlap of retangle title
Custom_item
{
id:third_item
anchors.top: second_item.bottom
}
}
content file Custom_item.qml
import QtQuick 2.5
Item
{
id:testComponent
Rectangle
{
width: 300
height: 60
id: text_content
color: "DarkGray"
opacity: 0.9
border.color: "blue"
border.width: 3
radius: 5
z:6
}
}
what am I doing wrong ?
Thanks
The problem lies within the dimensions of the objects your are anchoring to.
Though the Rectangles have a width and a height, the enclosing Item has none, so it is basically 0 pixels in height and width, while the Rectangle protrudes it.
If you don't have any reason for enclosing the Rectangle within the Item, I'd reccomend you, to take the Rectangle itself as the toplevel element of the file.
Reasons for having the Item might be those:
Hiding the Rectangles properties
Having multiple children for the Item that are logically siblings to the Rectangle
... other reasons might exist ;-)
Nevertheless, you need to make sure, that the toplevel item has always the right dimensions. So you should set the width and height, better the implicitWidth and implicitHeight in component declarations.
Example 1: Without an Item
import QtQuick 2.5
Rectangle {
id: root
width: 300
height: 60
color: "DarkGray"
opacity: 0.9
border.color: "blue"
border.width: 3
radius: 5
z:6
}
Example 2: With Item
import QtQuick 2.5
Item {
id:testComponent
implicitHeight: 60 // < This
implicitWidth: 300 // < and that are important to have the dimensions
Rectangle {
id: text_content
anchors.fill: parent
color: "DarkGray"
opacity: 0.9
border.color: "blue"
border.width: 3
radius: 5
z:6
}
}
You are anchoring all the Rectangle's to the Item hence you are not getting the desired result. Simple change the id of the top Rectangle as follows
Item
{
id: root
Rectangle
{
id:main_screen
...
}
}

Wrapping ListView inside rectangle

My problem is when i scroll ListView elements , the elements scroll over the rectangle border however i have wrapped the ListView inside the Rectangle.How can i make elements scroll without effecting the Rectangle borders.
Here are the result links
https://drive.google.com/file/d/0Bx616yTb6y_xQzNxRy1UcktrVzA/view?usp=sharing
https://drive.google.com/file/d/0Bx616yTb6y_xdl9CbWt4MTJ3Smc/view?usp=sharing
Following is the code
ListModel{
id: mod
}
Rectangle{
id:listviewrec
x: 347
y:644
width: 700
height: 91
radius: 4
border.color:"#7CC7FF"
border.width: 4
visible: true
ListView{
id:modlistview
width: listviewrec.width
height: listviewrec.height
clip: true
boundsBehavior: Flickable.DragOverBounds
spacing:25
model:mod
delegate: delegateimage
orientation: Qt.Horizontal
anchors.fill: listviewrec
}
}
Component{
id:delegateimage
Item{
id:imageitem
width:50
height:60
visible:true
Rectangle{
id:imagerec
x:10
y:6
width: 60
height:70
border.color: "#7CC7FF"
border.width: 5
radius: 2
visible:true
Image{
x: 3
y: 3
height : imagerec.height
visible: true
width : imagerec.width
anchors.fill: imagerec
source:fileUrl
}
}
}
}
I don't think qml has the concept of inner- and outer- rects as far as borders are concerned, (or if it does, borders are drawn in the inner-rect so children will be drawn on top).
Your best bet here is to probably do something like this:
Item {
id:listviewrec
x: 347
y:644
width: 700
height: 91
visible: true
ListView{
id:modlistview
width: listviewrec.width
height: listviewrec.height
clip: true
boundsBehavior: Flickable.DragOverBounds
spacing:25
model:mod
delegate: delegateimage
orientation: Qt.Horizontal
anchors.fill: listviewrec
}
Rectangle {
radius: 4
border.color:"#7CC7FF"
border.width: 4
color: 'transparent'
anchors.fill: parent
}
}
It simply draws a transparent rect with the border you want on top of the ListView
At the moment I am unable to run your code, but it seems a matter of removing explicit width and height settings, since you are using anchors.fill

Resources