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 1: Line 1:
=PySide <span class="caps">QML</span> Basic Tutorial 2: <span class="caps">QML</span> Components=
[[Category:LanguageBindings::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 <span class="caps">QML</span> code for Cell.qml:
Here is the QML code for Cell.qml:


==Walkthrough¶==
<code><br />import QtQuick 1.0


===The Cell Component¶===
Item {<br /> id: container<br /> property alias cellColor: rectangle.color<br /> signal clicked(color cellColor)


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.
width: 40; height: 25


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.
Rectangle {<br /> id: rectangle<br /> border.color: &quot;white&amp;quot;<br /> anchors.fill: parent<br /> }


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.
MouseArea {<br /> anchors.fill: parent<br /> onClicked: container.clicked(container.cellColor)<br /> }<br />}<br /></code>
 
== Walkthrough¶ ==
 
=== The Cell Component¶ ===
 
<code><br />Item {<br /> id: container<br /> property alias cellColor: rectangle.color<br /> signal clicked(color cellColor)
 
width: 40; height: 25<br /></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><br /> Rectangle {<br /> id: rectangle<br /> border.color: &quot;white&amp;quot;<br /> anchors.fill: parent<br /> }<br /></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><br /> MouseArea {<br /> anchors.fill: parent<br /> onClicked: container.clicked(container.cellColor)<br /> }<br /></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 49:
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 QML file, we use our Cell component to create the color picker:
 
<code><br />import QtQuick 1.0
 
Rectangle {<br /> id: page<br /> width: 500; height: 200<br /> color: &quot;lightgray&amp;quot;


In our main <span class="caps">QML</span> file, we use our Cell component to create the color picker:
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 /> }


We create the color picker by putting 6 cells with different colors in a grid.
Grid {<br /> id: colorPicker<br /> x: 4; anchors.bottom: page.bottom; anchors.bottomMargin: 4<br /> 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: &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>


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


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

Revision as of 09:07, 24 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:

<br />import QtQuick 1.0

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

width: 40; height: 25

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

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

Walkthrough¶

The Cell Component¶

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

width: 40; height: 25<br />

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.

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

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.

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

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:

<br />import QtQuick 1.0

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

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 /> }

Grid {<br /> id: colorPicker<br /> x: 4; anchors.bottom: page.bottom; anchors.bottomMargin: 4<br /> 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 />

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

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