PySide QML Tutorial Basic 2: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(Rename category "LanguageBindings::PySide" -> "PySide")
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
=PySide <span class="caps">QML</span> Basic Tutorial 2: <span class="caps">QML</span> Components=
 
 
[[Category:PySide]]
 
= PySide QML Basic Tutorial 2: QML Components =


This chapter adds a color picker to change the color of the text.
This chapter adds a color picker to change the color of the text.


Our color picker is made of six cells with different colors. To avoid writing the same code multiple times for each cell, we create a new Cell component. A component provides a way of defining a new type that we can re-use in other <span class="caps">QML</span> files. A <span class="caps">QML</span> component is like a black-box and interacts with the outside world through properties, signals and functions and is generally defined in its own <span class="caps">QML</span> file. The component’s filename must always start with a capital letter.
Our color picker is made of six cells with different colors. To avoid writing the same code multiple times for each cell, we create a new Cell component. A component provides a way of defining a new type that we can re-use in other QML files. A QML component is like a black-box and interacts with the outside world through properties, signals and functions and is generally defined in its own QML file. The component’s filename must always start with a capital letter.
 
Here is the QML code for Cell.qml:
 
<code>
import QtQuick 1.0
 
Item {
id: container
property alias cellColor: rectangle.color
signal clicked(color cellColor)


Here is the <span class="caps">QML</span> code for Cell.qml:
width: 40; height: 25


==Walkthrough¶==
Rectangle {
id: rectangle
border.color: "white"
anchors.fill: parent
}


===The Cell Component¶===
MouseArea {
anchors.fill: parent
onClicked: container.clicked(container.cellColor)
}
}
</code>


The root element of our component is an Item with the id container. An Item is the most basic visual element in <span class="caps">QML</span> and is often used as a container for other elements.
== Walkthrough¶ ==


We declare a cellColor property. This property is accessible from outside our component, this allows us to instantiate the cells with different colors. This property is just an alias to an existing property – the color of the rectangle that compose the cell.
=== The Cell Component¶ ===


We want our component to also have a signal that we call clicked with a cellColor parameter of type color. We will use this signal to change the color of the text in the main <span class="caps">QML</span> file later.
<code>
Item {
id: container
property alias cellColor: rectangle.color
signal clicked(color cellColor)
 
width: 40; height: 25
</code>
 
The root element of our component is an Item with the id container. An Item is the most basic visual element in QML and is often used as a container for other elements.
 
<code> property alias cellColor: rectangle.color</code>
 
We declare a cellColor property. This property is accessible from outside our component, this allows us to instantiate the cells with different colors. This property is just an alias to an existing property - the color of the rectangle that compose the cell.
 
<code> signal clicked(color cellColor)</code>
 
We want our component to also have a signal that we call clicked with a cellColor parameter of type color. We will use this signal to change the color of the text in the main QML file later.
 
<code>
Rectangle {
id: rectangle
border.color: "white"
anchors.fill: parent
}
</code>


Our cell component is basically a colored rectangle with the id rectangle.
Our cell component is basically a colored rectangle with the id rectangle.


The anchors.fill property is a convenient way to set the size of an element. In this case the rectangle will have the same size as its parent.
The anchors.fill property is a convenient way to set the size of an element. In this case the rectangle will have the same size as its parent.
<code>
MouseArea {
anchors.fill: parent
onClicked: container.clicked(container.cellColor)
}
</code>


In order to change the color of the text when clicking on a cell, we create a MouseArea element with the same size as its parent.
In order to change the color of the text when clicking on a cell, we create a MouseArea element with the same size as its parent.
Line 25: Line 80:
A MouseArea defines a signal called clicked. When this signal is triggered we want to emit our own clicked signal with the color as parameter.
A MouseArea defines a signal called clicked. When this signal is triggered we want to emit our own clicked signal with the color as parameter.


===The main <span class="caps">QML</span> file===
=== The main QML file ===


In our main <span class="caps">QML</span> file, we use our Cell component to create the color picker:
In our main QML file, we use our Cell component to create the color picker:


We create the color picker by putting 6 cells with different colors in a grid.
<code>
import QtQuick 1.0
 
Rectangle {
id: page
width: 500; height: 200
color: "lightgray"
 
Text {
id: helloText
text: "Hello world!"
y: 30
anchors.horizontalCenter: page.horizontalCenter
font.pointSize: 24; font.bold: true
}
 
Grid {
id: colorPicker
x: 4; anchors.bottom: page.bottom; anchors.bottomMargin: 4
rows: 2; columns: 3; spacing: 3


When the clicked signal of our cell is triggered, we want to set the color of the text to the cellColor passed as a parameter. We can react to any signal of our component through a property of the name ‘onSignalName’.
Cell { cellColor: "red"; onClicked: helloText.color = cellColor }
Cell { cellColor: "green"; onClicked: helloText.color = cellColor }
Cell { cellColor: "blue"; onClicked: helloText.color = cellColor }
Cell { cellColor: "yellow"; onClicked: helloText.color = cellColor }
Cell { cellColor: "steelblue"; onClicked: helloText.color = cellColor }
Cell { cellColor: "black"; onClicked: helloText.color = cellColor }
}
}
</code>


===Categories:===
We create the color picker by putting 6 cells with different colors in a grid.


* [[:Category:LanguageBindings|LanguageBindings]]
<code> Cell { cellColor: "red"; onClicked: helloText.color = cellColor }</code>
** [[:Category:LanguageBindings::PySide|PySide]]

Latest revision as of 03:30, 5 June 2016


PySide QML Basic Tutorial 2: QML Components

This chapter adds a color picker to change the color of the text.

Our color picker is made of six cells with different colors. To avoid writing the same code multiple times for each cell, we create a new Cell component. A component provides a way of defining a new type that we can re-use in other QML files. A QML component is like a black-box and interacts with the outside world through properties, signals and functions and is generally defined in its own QML file. The component’s filename must always start with a capital letter.

Here is the QML code for Cell.qml:

import QtQuick 1.0

Item {
 id: container
 property alias cellColor: rectangle.color
 signal clicked(color cellColor)

width: 40; height: 25

Rectangle {
 id: rectangle
 border.color: "white"
 anchors.fill: parent
 }

MouseArea {
 anchors.fill: parent
 onClicked: container.clicked(container.cellColor)
 }
}

Walkthrough¶

The Cell Component¶

Item {
 id: container
 property alias cellColor: rectangle.color
 signal clicked(color cellColor)

width: 40; height: 25

The root element of our component is an Item with the id container. An Item is the most basic visual element in QML and is often used as a container for other elements.

 property alias cellColor: rectangle.color

We declare a cellColor property. This property is accessible from outside our component, this allows us to instantiate the cells with different colors. This property is just an alias to an existing property - the color of the rectangle that compose the cell.

 signal clicked(color cellColor)

We want our component to also have a signal that we call clicked with a cellColor parameter of type color. We will use this signal to change the color of the text in the main QML file later.

 Rectangle {
 id: rectangle
 border.color: "white"
 anchors.fill: parent
 }

Our cell component is basically a colored rectangle with the id rectangle.

The anchors.fill property is a convenient way to set the size of an element. In this case the rectangle will have the same size as its parent.

 MouseArea {
 anchors.fill: parent
 onClicked: container.clicked(container.cellColor)
 }

In order to change the color of the text when clicking on a cell, we create a MouseArea element with the same size as its parent.

A MouseArea defines a signal called clicked. When this signal is triggered we want to emit our own clicked signal with the color as parameter.

The main QML file

In our main QML file, we use our Cell component to create the color picker:

import QtQuick 1.0

Rectangle {
 id: page
 width: 500; height: 200
 color: "lightgray"

Text {
 id: helloText
 text: "Hello world!"
 y: 30
 anchors.horizontalCenter: page.horizontalCenter
 font.pointSize: 24; font.bold: true
 }

Grid {
 id: colorPicker
 x: 4; anchors.bottom: page.bottom; anchors.bottomMargin: 4
 rows: 2; columns: 3; spacing: 3

Cell { cellColor: "red"; onClicked: helloText.color = cellColor }
 Cell { cellColor: "green"; onClicked: helloText.color = cellColor }
 Cell { cellColor: "blue"; onClicked: helloText.color = cellColor }
 Cell { cellColor: "yellow"; onClicked: helloText.color = cellColor }
 Cell { cellColor: "steelblue"; onClicked: helloText.color = cellColor }
 Cell { cellColor: "black"; onClicked: helloText.color = cellColor }
 }
}

We create the color picker by putting 6 cells with different colors in a grid.

 Cell { cellColor: "red"; onClicked: helloText.color = cellColor }