QML Gridlayout Problems - qt

I wish to make a prototype screen layout using QML and a GridLayout. I cannot get it to work. I have followed many previous examples here on Stackoverflow but nothing works satisfactorily and its clear I do not understand QML layouts and need some help. (DevEnv: Centos 7.5, Qt 5.11/QtCreator 4.7.1)
I wish to achieve this screen layout (ignore the colours)
Example 1
import QtQuick 2.11
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3
ApplicationWindow {
visible: true
visibility: "Maximized"
title: 'Title'
GridLayout {
id: mainLayout
anchors.fill: parent
rows: 8
columns: 12
Rectangle {
id: view_A
color: "lightgreen"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 11
Layout.rowSpan: 1
Layout.row: 1
Layout.column: 1
Text { text: "view_A" ; anchors.centerIn: parent }
}
Rectangle {
id: view_B
color: "yellow"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 5
Layout.rowSpan: 3
Layout.row: 2
Layout.column: 1
Text { text: "view_B"; anchors.centerIn: parent }
}
Rectangle {
id: view_C
color: "blue"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 4
Layout.rowSpan: 3
Layout.row: 2
Layout.column: 6
Text { text: "view_C" ; anchors.centerIn: parent }
}
Rectangle {
id: view_D
color: "blueviolet"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 2
Layout.rowSpan: 5
Layout.row: 2
Layout.column: 10
Text { text: "view_D" ; anchors.centerIn: parent }
}
Rectangle {
id: view_E
color: "lightblue"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 3
Layout.rowSpan: 4
Layout.row: 5
Layout.column: 1
Text { text: "view_E" ; anchors.centerIn: parent }
}
Rectangle {
id: view_F
color: "darkorange"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 6
Layout.rowSpan: 4
Layout.row: 5
Layout.column: 4
Text { text: "view_F" ; anchors.centerIn: parent }
}
Rectangle {
id: view_G
color: "seagreen"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 2
Layout.rowSpan: 2
Layout.row: 7
Layout.column: 10
Text { text: "view_G" ; anchors.centerIn: parent }
}
Rectangle {
id: view_H
color: "yellow"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 1
Layout.rowSpan: 8
Layout.row: 1
Layout.column: 12
Text { text: "view_H" ; anchors.centerIn: parent }
}
}
}
This solution comes close, however, the grid elements are not the sizes I specified. For example the top element id: view_A is far taller than desired, as is the width of id: view_H.
(Screenshot of results of Example 1 QML code execution )
Example 2:
I then tried the solution provided here (How can I create a QML GridLayout with items of proportionate sizes?).
This has come as close as I can get but there are ugly spaces between some elements.
// QML Code Example 2
import QtQuick 2.11
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3
ApplicationWindow {
visible: true
visibility: "Maximized"
title: 'Title'
GridLayout {
id: grid
anchors.fill: parent
rowSpacing: 0
columnSpacing: 0
rows: 8
columns: 12
property double colMultiplier: grid.width / grid.columns
property double rowMultiplier: grid.height / grid.rows;
function getElementHeight(element) {
return element.Layout.rowSpan * rowMultiplier
}
function getElementWidth(element) {
return element.Layout.columnSpan * colMultiplier
}
Rectangle {
id: view_A
color: "lightgreen"
Layout.columnSpan: 11
Layout.rowSpan: 1
Layout.row: 1
Layout.column: 1
Layout.preferredHeight: grid.getElementHeight(this)
Layout.preferredWidth: grid.getElementWidth(this)
Text { text: "view_A" ; anchors.centerIn: parent }
}
Rectangle {
id: view_B
color: "yellow"
Layout.columnSpan: 5
Layout.rowSpan: 3
Layout.row: 2
Layout.column: 1
Layout.preferredHeight: grid.getElementHeight(this)
Layout.preferredWidth: grid.getElementWidth(this)
Text { text: "view_B"; anchors.centerIn: parent }
}
Rectangle {
id: view_C
color: "blue"
Layout.columnSpan: 4
Layout.rowSpan: 3
Layout.row: 2
Layout.column: 6
Layout.preferredHeight: grid.getElementHeight(this)
Layout.preferredWidth: grid.getElementWidth(this)
Text { text: "view_C" ; anchors.centerIn: parent }
}
Rectangle {
id: view_D
color: "blueviolet"
Layout.columnSpan: 2
Layout.rowSpan: 5
Layout.row: 2
Layout.column: 10
Layout.preferredHeight: grid.getElementHeight(this)
Layout.preferredWidth: grid.getElementWidth(this)
Text { text: "view_D" ; anchors.centerIn: parent }
}
Rectangle {
id: view_E
color: "lightblue"
Layout.columnSpan: 3
Layout.rowSpan: 4
Layout.row: 5
Layout.column: 1
Layout.preferredHeight: grid.getElementHeight(this)
Layout.preferredWidth: grid.getElementWidth(this)
Text { text: "view_E" ; anchors.centerIn: parent }
}
Rectangle {
id: view_F
color: "darkorange"
Layout.columnSpan: 6
Layout.rowSpan: 4
Layout.row: 5
Layout.column: 4
Layout.preferredHeight: grid.getElementHeight(this)
Layout.preferredWidth: grid.getElementWidth(this)
Text { text: "view_F" ; anchors.centerIn: parent }
}
Rectangle {
id: view_G
color: "seagreen"
Layout.columnSpan: 2
Layout.rowSpan: 2
Layout.row: 7
Layout.column: 10
Layout.preferredHeight: grid.getElementHeight(this)
Layout.preferredWidth: grid.getElementWidth(this)
Text { text: "view_G" ; anchors.centerIn: parent }
}
Rectangle {
id: view_H
color: "yellow"
Layout.columnSpan: 1
Layout.rowSpan: 8
Layout.row: 1
Layout.column: 12
Layout.preferredHeight: grid.getElementHeight(this)
Layout.preferredWidth: grid.getElementWidth(this)
Text { text: "view_H" ; anchors.centerIn: parent }
}
}
}
So as can be seen in the resultant screenshot that ther are spaces between some elements that look terrible.
So, if there are any QML experts that can help solve this issue I would really like to hear from you.
kind regards ...

You can add Layout.preferedWidth and Layout.preferedHeight to fix this
GridLayout {
id: mainLayout
anchors.fill: parent
rows: 8
columns: 12
Rectangle {
id: view_A
color: "lightgreen"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 11
Layout.rowSpan: 1
Layout.preferredWidth: 11
Layout.preferredHeight: 1
Layout.row: 1
Layout.column: 1
Text { text: "view_A" ; anchors.centerIn: parent }
}
Rectangle {
id: view_B
color: "yellow"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 5
Layout.rowSpan: 3
Layout.preferredWidth: 5
Layout.preferredHeight: 3
Layout.row: 2
Layout.column: 1
Text { text: "view_B"; anchors.centerIn: parent }
}
Rectangle {
id: view_C
color: "blue"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 4
Layout.rowSpan: 3
Layout.preferredWidth: 4
Layout.preferredHeight: 3
Layout.row: 2
Layout.column: 6
Text { text: "view_C" ; anchors.centerIn: parent }
}
Rectangle {
id: view_D
color: "blueviolet"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 2
Layout.rowSpan: 5
Layout.preferredWidth: 2
Layout.preferredHeight: 5
Layout.row: 2
Layout.column: 10
Text { text: "view_D" ; anchors.centerIn: parent }
}
Rectangle {
id: view_E
color: "lightblue"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 3
Layout.rowSpan: 4
Layout.preferredWidth: 3
Layout.preferredHeight: 4
Layout.row: 5
Layout.column: 1
Text { text: "view_E" ; anchors.centerIn: parent }
}
Rectangle {
id: view_F
color: "darkorange"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 6
Layout.rowSpan: 4
Layout.preferredWidth: 6
Layout.preferredHeight: 4
Layout.row: 5
Layout.column: 4
Text { text: "view_F" ; anchors.centerIn: parent }
}
Rectangle {
id: view_G
color: "seagreen"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 2
Layout.rowSpan: 2
Layout.preferredWidth: 2
Layout.preferredHeight: 2
Layout.row: 7
Layout.column: 10
Text { text: "view_G" ; anchors.centerIn: parent }
}
Rectangle {
id: view_H
color: "yellow"
Layout.fillHeight: true
Layout.fillWidth: true
Layout.columnSpan: 1
Layout.rowSpan: 8
Layout.preferredWidth: 1
Layout.preferredHeight: 8
Layout.row: 1
Layout.column: 12
Text { text: "view_H" ; anchors.centerIn: parent }
}
}
Result :

Related

QML how to use a dynamic topmargin correctly

I want to enlarge or reduce a button depending on the presence of an icon. The buttons are in a rectangle and I would like to realize it with the top margin, because the buttons also have rounded corners and only the top corners should be visible.
The goal is a representation like this
If I set the topmargin fix to 10 it looks good
If I calculate the value depending on whether an icon is present, the buttons without icon are correctly displayed deeper, but the buttons with icon sit too deep
Like the picture before the topmargin for the icon buttons is 10 but they are moved lower.
Any idea - the code for display.qml is reduced and does not show all the properties. if they are needed i will add them.
FooterButton.qml
Rectangle {
id: button
property string p_identity
property string p_icon
property string p_source
property string p_backgroundColor
property int p_topmargin: 10
height: 70
width: 80
Layout.leftMargin: 25
Layout.topMargin: p_topmargin
color: p_backgroundColor
radius: 10
border.color: "black"
border.width: 0
Connections {
target: m_screen;
onScreenChanged: {
p_icon = m_screen.getButtonIcon(p_identity)
p_source = (!p_icon || p_icon.length === 0) ? "" : "image://iconprovider/" + p_icon)
// p_topmargin = 10
p_topmargin = (!p_icon || p_icon.length === 0) ? 45 : 10
}
}
Footer.qml
Item {
id: footer
property string p_footerBackgroundColor: "yellow" //m_config.getColor(Colors.FooterBackground)
property string p_buttonBackgroundColor: m_config.getColor(Colors.ButtonBackground)
Rectangle { anchors.fill: parent; x: footer.x; y: footer.y; width: footer.width; height: footer.height; color: p_footerBackgroundColor
RowLayout{ anchors.fill: parent
FooterButton{ p_identity: "FB1"; p_backgroundColor: p_buttonBackgroundColor }
FooterButton{ p_identity: "FB2"; p_backgroundColor: p_buttonBackgroundColor }
FooterButton{ p_identity: "FB3"; p_backgroundColor: p_buttonBackgroundColor }
FooterButton{ p_identity: "FB4"; p_backgroundColor: p_buttonBackgroundColor }
FooterButton{ p_identity: "FB5"; p_backgroundColor: p_buttonBackgroundColor }
}
}
Display.qml
Item {
id: display
Header { x: 0; y: 0; width: display.width; height: p_headerHeight; visible: p_headerVisible; color: p_backgroundColor; p_buttonColor: p_buttonBackgroundColor }
Left { x: 0; y: p_headerHeight; width: p_borderWidth; height: p_contentHeight; color: "blue" }
Right { x: display.width - p_encoderWidth; y: p_headerHeight; width: p_encoderWidth; height: p_contentHeight; p_color: "magenta" }
Footer { x: 0; y: display.height - p_footerHeight; width: display.width; height: p_footerHeight; visible: p_footerVisible }
just making sure you're not over-engineering your problem, but, I want to point out that the standard Button has both icon support and the ability to change the background to a rounded Rectangle:
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
footer: Frame {
background: Rectangle {
color: "yellow"
}
RowLayout {
anchors.horizontalCenter: parent.horizontalCenter
spacing: 20
AppButton {
//icon.source: "hammer.svg"
}
AppButton {
//icon.source: "hammer.svg"
}
AppButton {
icon.source: "hammer.svg"
}
AppButton {
//icon.source: "hammer.svg"
}
AppButton {
icon.source: "check.svg"
}
}
}
}
// AppButton.qml
import QtQuick
import QtQuick.Controls
Button {
width: 100
height: 100
background: Rectangle {
color: pressed ? palette.mid : palette.button
radius: 20
}
icon.source: "blank.svg"
icon.width: 64
icon.height: 64
}
// blank.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
</svg>
// hammer.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path d="M29.64 25.462c-1.186-1.62-3.535-4.176-6.254-7.136-2.657-2.893-5.671-6.173-8.066-9.11a3.883 3.883 0 0 1-1.044-1.531 6.899 6.899 0 0 0-.215-1.271 3.427 3.427 0 0 1-.08-.348 7.985 7.985 0 0 1 3.153-1.61 25.43 25.43 0 0 1 4.095-.527l1.08-.043-1.14-1.239-.148-.035a24.293 24.293 0 0 0-5.123-.606A13.096 13.096 0 0 0 7.53 4.82c-.225.2-1.433 1.478-1.338 2.334.078.73-.212.949-.792 1.383a8.35 8.35 0 0 0-.558.444c-1.468-.125-1.92.252-3.014 1.16l-.39.32-.095.105a1.472 1.472 0 0 0-.277 1.24 7.214 7.214 0 0 0 2.294 3.029 2.25 2.25 0 0 0 2.404-.483 18.003 18.003 0 0 0 1.577-2.018 2.67 2.67 0 0 1 1.633-1.26 12.815 12.815 0 0 1 2.588.88c.11.046.2.077.277.104.05.018.111.032.116.036 4.108 5.004 6.896 8.936 8.93 11.807 1.401 1.976 2.413 3.404 3.3 4.412l.912 1.038a1.935 1.935 0 0 0 1.362.651l.078.001a1.939 1.939 0 0 0 1.334-.534l1.548-1.486a1.927 1.927 0 0 0 .22-2.52zM12.059 11.028l-.029.034c-.03-.012-.052-.018-.088-.033a10.285 10.285 0 0 0-3-.954 3.577 3.577 0 0 0-2.454 1.738 21.031 21.031 0 0 1-1.375 1.786c-.605.434-.936.519-1.313.338a6.931 6.931 0 0 1-1.792-2.446.85.85 0 0 1 .125-.305l.334-.275c1.045-.867 1.228-1.021 2.299-.933a1.02 1.02 0 0 0 .738-.247A7.72 7.72 0 0 1 6 9.337a2.27 2.27 0 0 0 1.186-2.288A3.785 3.785 0 0 1 8.19 5.571a12.232 12.232 0 0 1 7.706-2.565 20.9 20.9 0 0 1 2.624.178c-.523.076-1.076.173-1.614.298A9.024 9.024 0 0 0 13.34 5.3a1.176 1.176 0 0 0-.25 1.356 5.831 5.831 0 0 1 .19 1.1 3.345 3.345 0 0 0 .842 1.625 9.48 9.48 0 0 0-.994.683 7.036 7.036 0 0 0-1.068.964zm16.668 16.234l-1.547 1.485a.945.945 0 0 1-.678.256.924.924 0 0 1-.652-.312l-.912-1.038c-.853-.97-1.905-2.452-3.236-4.33-2.018-2.848-4.78-6.742-8.838-11.696a6.433 6.433 0 0 1 .875-.772 8.145 8.145 0 0 1 .964-.66l.09-.05C17.14 13 20.06 16.182 22.65 19.001c2.7 2.939 5.032 5.477 6.184 7.051a.923.923 0 0 1-.106 1.209z"/><path fill="none" d="M0 0h32v32H0z"/></svg>
// check.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path d="M13.5 22.142L7.59 16.42l.636-.636L13.5 20.87 26.721 7.8l.637.637z"/><path fill="none" d="M0 0h32v32H0z"/></svg>
You can Try it Online!

How to apply Gradientcolor to ArcItem Strokecolor?

I would like to have the ArcItem in Gradientcolor. The Stroke color is not possible to set in Qt Design Studio to gradient, then I tried to use ColorOverlay, but this doesn't work.
How could archive this?
import QtQuick.Studio.Effects 1.0
import QtQuick.Shapes 1.0
import QtQuick.Timeline 1.0
Rectangle {
id: rectangle
width: Constants.width
height: Constants.height
color: "#000000"
ArcItem {
id: arc
x: 0
y: 0
width: 331
height: 327
outlineArc: false
capStyle: 0
antialiasing: true
strokeStyle: 1
end: 145
strokeWidth: 20
begin: -145
strokeColor: "#f72e2e" // --> This in gradient color
fillColor: "#00000000"
ColorOverlayItem {
id: colorOverlay
x: 0
y: 0
width: 331
height: 319
}
}
The following exemple should works with Qt5 + in addition you'll have a fancy glow effect
You could play arround with it.
import QtQuick 2.15
import QtQuick.Controls 2.15
import smartDash 1.0
import QtQuick.Studio.Components 1.0
import QtQuick.Shapes 1.0
import QtQuick.Studio.Effects 1.0
import QtQuick3D 1.15
import QtGraphicalEffects 1.0
Rectangle {
width: 300
height: 300
color: "#262626"
Item {
x: 0
y: 0
width: 300
height: 300
ArcItem {
id: glowingArc
x: 20
y: 20
width: 255
height: 255
opacity: 1
dashPattern: [0.2, 0.3, 0.1, 0.2]
strokeStyle: 1
arcWidthBegin: 0
arcWidth: 0
clip: true
dashOffset: 2
end: 0
begin: 360
strokeWidth: 13
radiusInnerAdjust: 0
outlineArc: false
capStyle: 0
fillColor: "#00000000"
strokeColor: "#b3cc9e00"
visible: false
}
Glow {
x: 2
y: 2
anchors.fill: glowingArc
radius: 19
samples: 17
color: "#dbbb1f"
source: glowingArc
spread: -0.4
}
ConicalGradient {
id: gradient
anchors.fill: glowingArc
angle: -360
gradient: Gradient {
GradientStop {
position: 0.1
color: "black"
}
GradientStop {
position: 0.0
color: "transparent"
}
}
visible: false
}
OpacityMask {
anchors.fill: gradient
source: glowingArc
maskSource: gradient
invert: false
}
}
}

GridLayout spacing problems when using Material Design

SITUATION
I want to remove the row spacing from my GridLayout.
Here is my main.qml:
ApplicationWindow {
Root { id: root }
Material.theme: Material.Dark
Material.accent: Material.Purple
visible: true
title: root.title
width: 300
height: 400
minimumWidth: width
minimumHeight: height
maximumWidth: width
maximumHeight: height
GridLayout {
anchors.fill: parent
columnSpacing: 0
rowSpacing: 0
columns: 4
rows: 5
TextField {
Layout.fillWidth: true
Layout.columnSpan: 4
Layout.column: 0
Layout.row: 0
padding: 10
implicitHeight: 70
readOnly: true
text: root.input
}
CButton {
Layout.column: 0
Layout.row: 1
text: "7"
}
CButton {
Layout.column: 1
Layout.row: 1
text: "8"
}
CButton {
Layout.column: 2
Layout.row: 1
text: "9"
}
CButton {
Layout.column: 3
Layout.row: 1
text: "/"
}
CButton {
Layout.column: 0
Layout.row: 2
text: "4"
}
CButton {
Layout.column: 1
Layout.row: 2
text: "5"
}
CButton {
Layout.column: 2
Layout.row: 2
text: "6"
}
CButton {
Layout.column: 3
Layout.row: 2
text: "*"
}
CButton {
Layout.column: 0
Layout.row: 3
text: "1"
}
CButton {
Layout.column: 1
Layout.row: 3
text: "2"
}
CButton {
Layout.column: 2
Layout.row: 3
text: "3"
}
CButton {
Layout.column: 3
Layout.row: 3
text: "-"
}
CButton {
Layout.column: 0
Layout.row: 4
text: "C"
}
CButton {
Layout.column: 1
Layout.row: 4
text: "0"
}
CButton {
Layout.column: 2
Layout.row: 4
text: "="
}
CButton {
Layout.column: 3
Layout.row: 4
text: "+"
}
}
}
And here my CButton.qml:
Button {
property Root context: root
Layout.fillWidth: true
Layout.fillHeight: true
onClicked: {
if (text == "=")
context.calculateInput();
else if (text == "C")
context.clearInput();
else
context.changeInput(text);
}
}
PROBLEM
As you can see in the image, there is a margin between the buttons. When I remove the Material Design from my app, it works, but with Material Design it has row spacing for some reason. Is there a way to fix this behavior or I'm supposed to create my custom design?
NOTE: I don't want to add negative row spacing in my GridLayout to fix this "bug".
After inspection of the Button.qml file from the Qt folder in the Material subfolder, I found the base class have 4 properties: topInset, bottomInset, leftInset and rightInset. By setting these you can get the desired effect:
CButton.qml:
Button {
property Root context: root
Layout.fillWidth: true
Layout.fillHeight: true
topInset: 0
bottomInset: 0
onClicked: {
if (text == "=")
context.calculateInput();
else if (text == "C")
context.clearInput();
else
context.changeInput(text);
}
}

GridLayout in QML how to constraint Rectangle in a cell

I am using the below code hoping to set my Rectangle in the top middle of the Item.
import QtQuick 2.0
import QtQuick.Layouts 1.3
Item {
GridLayout {
id: gridLayout
anchors.fill: parent
columns: 5
rows: 7
Rectangle {
id: rectangle
Layout.fillHeight: true
Layout.fillWidth: true
Layout.row: 0
Layout.column: 2
Layout.rowSpan: 1
Layout.columnSpan: 2
color: "#ffffff"
}
}
}
Instead the Rectangle is filling the whole item.
I see so everything is relative in GridLayout and if things don't properly take a specified span then it's a "manure" show.
The following works for me.
import QtQuick 2.0
import QtQuick.Layouts 1.3
Item {
GridLayout {
id: gridLayout
anchors.fill: parent
Rectangle {
id: top_center
Layout.fillHeight: true
Layout.fillWidth: true
Layout.row: 0
Layout.column: 2
Layout.rowSpan: 1
Layout.columnSpan: 1
color: "#ffffff"
}
Rectangle {
Layout.fillHeight: true
Layout.fillWidth: true
Layout.row: 1
Layout.column: 0
Layout.rowSpan: 3
Layout.columnSpan: 1
color: "lightblue"
}
Rectangle {
Layout.fillHeight: true
Layout.fillWidth: true
Layout.row: 1
Layout.column: 1
Layout.rowSpan: 3
Layout.columnSpan: 6
color: "white"
}
Rectangle {
Layout.fillHeight: true
Layout.fillWidth: true
Layout.row: 4
Layout.column: 0
Layout.rowSpan: 1
Layout.columnSpan: 6
color: "lightblue"
}
}
}

QML fit screen on all resolutions

Hi all I have problem with my QML code.
I made mistake and I went to put certain size to elements and now I have problem when putting app on other devices.
I will paste you my code where I have width and height so you can change it to show me how to work with dynamic resizeing.
I need to say that I am calling qml file from qt with this code:
QDeclarativeView *view= new QDeclarativeView;
ui->setupUi(this);
setCentralWidget(view);
QDeclarativeContext *ctxt = view->rootContext();
ctxt->setContextProperty("funkcije",this);
ctxt->setContextProperty("myModel", QVariant::fromValue(MainWindow::dataList));
view->setSource(QUrl("qrc:/gui.qml"));
view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
showFullScreen();
And here goes my QML code:
Rectangle {
id:window
width: 602
height: 1000
anchors.fill: parent
radius: 0
.....
ListView {
id: listview1
x: 0
y: 219
// width: 574
// height: 967
width: window.width
height: window.height
visible: true
keyNavigationWraps: false
boundsBehavior: Flickable.DragAndOvershootBounds
opacity: 1
maximumFlickVelocity: 2500
anchors.leftMargin: 0
highlightMoveSpeed: 489
contentWidth: 0
preferredHighlightEnd: 2
spacing: 5
highlightRangeMode: ListView.NoHighlightRange
snapMode: ListView.SnapToItem
anchors.bottomMargin: 0
anchors.rightMargin: 0
anchors.topMargin: 219
anchors.fill: parent
model: myModel
delegate:Component {
//id: contactDelegate
Item {
id:it;
property variant myData: model
width: 574; height: 220
Column {
id:col
x: 12
y: 0
width: 561
height: 164
smooth: true
anchors.rightMargin: 0
anchors.bottomMargin: 7
anchors.leftMargin: 13
anchors.topMargin: 7
anchors.fill: parent
spacing: 15
......
highlight: Rectangle
{
width: 600
height: 222
color:"black"; radius: 5; opacity: 0.7
focus: true
}
}
Rectangle {
id: rectangle1
x: 0
y: 0
width: 602
height: 219
......
//dodaj korisnika
Flipable {
id: flipable
x: 6
y: 32
width: 173
height: 179
.......
MouseArea {
x: 10
y: 9
width: 146
height: 150
anchors.rightMargin: 7
anchors.leftMargin: 10
anchors.topMargin: 9
anchors.bottomMargin: 9
hoverEnabled: false
anchors.fill: parent
........
//Brisanje korisnika
Flipable {
id: flipable1
x: 408
y: 32
width: 175
height: 179
.......
MouseArea {
x: 7
y: 9
width: 153
height: 151
anchors.rightMargin: 8
anchors.leftMargin: 7
anchors.topMargin: 9
anchors.bottomMargin: 8
hoverEnabled: false
anchors.fill: parent
.......
//promjeni korisnika
Flipable {
id: flipable2
x: 208
y: 32
width: 176
height: 179
.......
MouseArea {
x: 7
y: 9
width: 73
height: 76
anchors.rightMargin: 7
anchors.leftMargin: 7
anchors.topMargin: 9
anchors.bottomMargin: 9
hoverEnabled: false
anchors.fill: parent
.......
Text {
id: text1
x: 200
y: 212
font.pixelSize: 12
opacity: 0
}
Rectangle {
id: rectangle2
x: 259
y: 510
width: 200
height: 200
color: "#ffffff"
opacity: 0
}
TextInput {
id: text_input1
x: 331
y: 233
width: 80
height: 20
font.pixelSize: 12
opacity: 0
}
Text {
id: text2
x: 136
y: 228
font.pixelSize: 12
opacity: 0
}
Text {
id: text3
x: 152
y: 314
font.pixelSize: 12
opacity: 0
}
Rectangle {
id: rectangle3
x: 256
y: 293
width: 200
height: 200
color: "#ffffff"
opacity: 0
}
Rectangle {
id: rectangle4
x: 339
y: 787
width: 200
height: 200
color: "#ffffff"
opacity: 0
}
Rectangle {
id: rectangle5
x: 270
y: 456
width: 200
height: 200
color: "#ffffff"
opacity: 0
}
TextInput {
id: text_input2
x: 269
y: 316
width: 80
height: 20
font.pixelSize: 12
opacity: 0
}
TextInput {
id: text_input3
x: 269
y: 401
width: 80
height: 20
font.pixelSize: 12
opacity: 0
}
TextInput {
id: text_input4
x: 269
y: 495
width: 80
height: 20
font.pixelSize: 12
opacity: 0
}
TextInput {
id: text_input5
x: 143
y: 465
width: 80
height: 20
text: qsTr("text")
font.pixelSize: 12
opacity: 0
}
states: [
State {
name: "State1"
PropertyChanges {
target: listview1
x: 0
y: 1049
width: 574
height: 967
visible: false
anchors.topMargin: 1049
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.leftMargin: 0
}
PropertyChanges {
target: rectangle1
x: 0
y: 0
width: 602
height: 253
visible: true
}
PropertyChanges {
target: text1
x: 187
y: 253
width: 247
height: 71
color: "#c48d17"
text: qsTr("Unesite novog korisnika")
styleColor: "#e61717"
style: "Raised"
font.pixelSize: 31
font.family: "Lucida Handwriting"
verticalAlignment: "AlignVCenter"
horizontalAlignment: "AlignHCenter"
opacity: 1
}
PropertyChanges {
target: rectangle2
x: 251
y: 353
width: 258
height: 42
color: "#777e79"
radius: 15
smooth: true
opacity: 1
}
PropertyChanges {
target: text_input1
x: 251
y: 360
width: 258
height: 29
font.pixelSize: 17
font.family: "Lucida Handwriting"
horizontalAlignment: "AlignHCenter"
opacity: 1
}
PropertyChanges {
target: text2
x: 127
y: 362
width: 101
height: 25
color: "#fd0606"
text: qsTr("Ime")
style: "Raised"
font.family: "Lucida Handwriting"
font.pixelSize: 22
verticalAlignment: "AlignVCenter"
horizontalAlignment: "AlignHCenter"
opacity: 1
}
PropertyChanges {
target: text3
x: 119
y: 519
color: "#f70606"
text: qsTr("Prezime")
style: "Raised"
font.pixelSize: 20
font.family: "Lucida Handwriting"
verticalAlignment: "AlignVCenter"
horizontalAlignment: "AlignHCenter"
opacity: 1
}
PropertyChanges {
target: rectangle3
x: 251
y: 514
width: 258
height: 40
color: "#777e79"
radius: 15
opacity: 1
}
PropertyChanges {
target: rectangle4
x: 251
y: 669
width: 258
height: 38
color: "#777e79"
radius: 15
opacity: 1
}
PropertyChanges {
target: rectangle5
x: 251
y: 823
width: 258
height: 36
color: "#777e79"
radius: 15
opacity: 1
}
PropertyChanges {
target: text_input2
x: 251
y: 519
width: 258
height: 29
font.family: "Lucida Handwriting"
font.pixelSize: 17
horizontalAlignment: "AlignHCenter"
opacity: 1
}
PropertyChanges {
target: text_input3
x: 251
y: 674
width: 258
height: 29
horizontalAlignment: "AlignHCenter"
font.pixelSize: 17
font.family: "Lucida Handwriting"
opacity: 1
}
PropertyChanges {
target: text_input4
x: 251
y: 827
width: 258
height: 29
font.family: "Lucida Handwriting"
font.pixelSize: 17
horizontalAlignment: "AlignHCenter"
opacity: 1
}
PropertyChanges {
target: text4
x: 127
y: 669
width: 85
height: 31
color: "#f70606"
text: qsTr("Broj")
style: "Raised"
font.family: "Lucida Handwriting"
font.pixelSize: 20
horizontalAlignment: "AlignHCenter"
verticalAlignment: "AlignVCenter"
opacity: 1
}
]
}
I have put all the code with width and height so you can see what I have done wrong. And please if someone can say me how to make it dynamically fill.
It's unfortunately tricky to get perfect, as it's likely that as the screen size shrinks you might actually want the buttons to be bigger and to drop content from the screen to ensure the user can access and read everything ok.
But the general approach is actually to set a scale factor in the C++ side:
ctxt->setContextProperty("scale", /* put calculated scale factor here */);
And then on the QML side, use that everyone to scale all the objects:
Rectangle {
id:window
width: 602 * scale
height: 1000 * scale
That way you can adjust the scale variable to change everything's size. Having said that, many people end up with different QML files depending on the platform size though.
I recommend you read the Scalability page in the current Qt (4.8) documentation: it is on exactly this topic.
It recommends these techniques (I'm quoting the page here), and then supplies much more detail.
Create separate top-level layout definitions for each form factor.
Keep the layouts small and let components scale relative to their
immediate parent.
Define device independent measurements, such as dp (device
independent pixels), and use these to scale components and for layout
measurement.
Define layouts in a proportional way using the built-in layout
features of QML.
Update 2014-11-18 This and video article looks very useful indeed:
Supporting Multiple Screen Sizes & Screen Densities with Qt and V-Play
Update 2017-01-24 There is an updated Qt 5.8 version of the Scalability page mentioned above.
Changing the device, you are actually changing the screen pixel density. If you have a device with low quality display it will have lower number of pixels per inch or pixels per centimeter than a device with high quality display.
Understanding this we can easily devise a method to scale our content according to the pixel density. For example in my case I have a laptop on which I use Qt creator. It has pixel density of 4. But my android phone is of high quality which has density of 16(4 times more than my Laptop). It means if an item has a width 'X' and height 'Y' when displaying on my Laptop, on my phone it will appear with a width 'X'/4 and height 'Y'/4 . Thus I have to scale height and width by 4.
Now, How to implement this? In QML we have a property "pixelDensity" under object Screen which will give you the pixel density of the screen where you are running your application. Dividing this by the screen density of screen where you tested your application will give you the scale factor. So now you don't need to worry about other devices you have just find the pixel density of screen you are currently working on.
Following code works perfectly for me.
property int default_pix_density: 4 //pixel density of my current screen
property int scale_factor: Screen.pixelDensity/default_pix_density
Rectangle
{
width: 50*scale_factor
height: 20*scale_factor
}

Resources