Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

PySide QML Tutorial Basic 2: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 9: Line 9:
Here is the QML code for Cell.qml:
Here is the QML code for Cell.qml:


<code><br />import QtQuick 1.0
<code>
import QtQuick 1.0


Item {<br /> id: container<br /> property alias cellColor: rectangle.color<br /> signal clicked(color cellColor)
Item {
id: container
property alias cellColor: rectangle.color
signal clicked(color cellColor)


width: 40; height: 25
width: 40; height: 25


Rectangle {<br /> id: rectangle<br /> border.color: &quot;white&amp;quot;<br /> anchors.fill: parent<br /> }
Rectangle {
id: rectangle
border.color: "white"
anchors.fill: parent
}


MouseArea {<br /> anchors.fill: parent<br /> onClicked: container.clicked(container.cellColor)<br /> }<br />}<br /></code>
MouseArea {
anchors.fill: parent
onClicked: container.clicked(container.cellColor)
}
}
</code>


== Walkthrough¶ ==
== Walkthrough¶ ==
Line 23: Line 36:
=== The Cell Component¶ ===
=== The Cell Component¶ ===


<code><br />Item {<br /> id: container<br /> property alias cellColor: rectangle.color<br /> signal clicked(color cellColor)
<code>
Item {
id: container
property alias cellColor: rectangle.color
signal clicked(color cellColor)


width: 40; height: 25<br /></code>
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.
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.
Line 37: Line 55:
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.
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><br /> Rectangle {<br /> id: rectangle<br /> border.color: &quot;white&amp;quot;<br /> anchors.fill: parent<br /> }<br /></code>
<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.
Line 43: Line 67:
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><br /> MouseArea {<br /> anchors.fill: parent<br /> onClicked: container.clicked(container.cellColor)<br /> }<br /></code>
<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 53: Line 82:
In our main QML 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:


<code><br />import QtQuick 1.0
<code>
import QtQuick 1.0


Rectangle {<br /> id: page<br /> width: 500; height: 200<br /> color: &quot;lightgray&amp;quot;
Rectangle {
id: page
width: 500; height: 200
color: "lightgray"


Text {<br /> id: helloText<br /> text: &quot;Hello world!&quot;<br /> y: 30<br /> anchors.horizontalCenter: page.horizontalCenter<br /> font.pointSize: 24; font.bold: true<br /> }
Text {
id: helloText
text: "Hello world!"
y: 30
anchors.horizontalCenter: page.horizontalCenter
font.pointSize: 24; font.bold: true
}


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


Cell { cellColor: &quot;red&amp;quot;; onClicked: helloText.color = cellColor }<br /> Cell { cellColor: &quot;green&amp;quot;; onClicked: helloText.color = cellColor }<br /> Cell { cellColor: &quot;blue&amp;quot;; onClicked: helloText.color = cellColor }<br /> Cell { cellColor: &quot;yellow&amp;quot;; onClicked: helloText.color = cellColor }<br /> Cell { cellColor: &quot;steelblue&amp;quot;; onClicked: helloText.color = cellColor }<br /> Cell { cellColor: &quot;black&amp;quot;; onClicked: helloText.color = cellColor }<br /> }<br />}<br /></code>
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>


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


<code> Cell { cellColor: &quot;red&amp;quot;; onClicked: helloText.color = cellColor }<code>
<code> Cell { cellColor: "red"; onClicked: helloText.color = cellColor }<code>

Revision as of 09:37, 25 February 2015


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<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.

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 }