Qt for Python Tutorial HelloQML: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
[[Category:LanguageBindings::PySide]]
'''English''' [[Hello-World-in-PySide-and-QtQuick-Korean|한국어]] [[Hello-World-in-PySide-and-QtQuick-Japanese|日本語]]
'''English''' [[Hello-World-in-PySide-and-QtQuick-Korean|한국어]] [[Hello-World-in-PySide-and-QtQuick-Japanese|日本語]]


=Your first application using PySide and QtQuick/QML=
= Your first application using PySide and QtQuick/QML =
 
QML is a declarative language designed to describe the user interface of a program: both what it looks like, and how it behaves. In QML, a user interface is specified as a tree of objects with properties. In this tutorial we will show how you can make a simple '''Hello World''' application with PySide and QML.
 
A PySide/QML application consists, at least, of two different files - a file with the QML description of the user interface, and a python file which loads the qml file. To avoid problems for now, don't forget to save both files in the same folder.
 
Here is a simple QML file, called '''view.qml''':


<span class="caps">QML</span> is a declarative language designed to describe the user interface of a program: both what it looks like, and how it behaves. In <span class="caps">QML</span>, a user interface is specified as a tree of objects with properties. In this tutorial we will show how you can make a simple '''Hello World''' application with PySide and <span class="caps">QML</span>.
<code><br />import QtQuick 1.0


A PySide/QML application consists, at least, of two different files – a file with the <span class="caps">QML</span> description of the user interface, and a python file which loads the qml file. To avoid problems for now, don’t forget to save both files in the same folder.
Rectangle {<br /> width: 200<br /> height: 200<br /> color: &quot;red&amp;quot;


Here is a simple <span class="caps">QML</span> file, called '''view.qml''':
Text {<br /> text: &quot;Hello World&amp;quot;<br /> anchors.centerIn: parent<br /> }<br />}<br /></code>


We start by importing QtQuick 1.0, since in theory, QtQuick has a different release schedule than Qt/PySide.
We start by importing QtQuick 1.0, since in theory, QtQuick has a different release schedule than Qt/PySide.


The rest of the <span class="caps">QML</span> code is pretty straightforward for those who have previously used <span class="caps">HTML</span> or <span class="caps">XML</span> files. Basically, we are creating a red rectangle with the size 200*200 and, inside that rectangle, we are adding a Text element which says '''Hello World'''. The code '''anchors.centerIn: parent''' just makes the text appear centered in relation to its immediate parent.
The rest of the QML code is pretty straightforward for those who have previously used HTML or XML files. Basically, we are creating a red rectangle with the size 200*200 and, inside that rectangle, we are adding a Text element which says '''Hello World'''. The code '''anchors.centerIn: parent''' just makes the text appear centered in relation to its immediate parent.


Now, let’s see how the code looks on the PySide. Let’s call it '''main.py''':
Now, let's see how the code looks on the PySide. Let's call it '''main.py''':


If you are already familiar with PySide and have followed our tutorials, much of this code is already familiar. The only novelties are that you must import QDeclarativeView and set the source of the QDeclarativeView object to the <span class="caps">URL</span> of your <span class="caps">QML</span> file. Then, as any Qt widget, you call '''QDeclarativeView.show()'''.
<code><br />#!/usr/bin/env python<br /># <s>'''- coding: utf-8 -'''</s>


'''Hint:''' If you are programming for desktop, you should consider adding '''view.setResizeMode(QDeclarativeView.SizeRootObjectToView)''' before showing the view. This will force the outer <span class="caps">QML</span> rectangle to resize along with the outer window.
import sys<br />from PySide.QtCore import *<br />from PySide.QtGui import *<br />from PySide.QtDeclarative import QDeclarativeView


Take your time exploring this example. You can try to do basic things as changing the colors, the text, or set other properties to each element, setting a radius on the rectangle color. Check the elements and their properties in [http://doc.qt.io/qt-4.8/qdeclarativeelements.html <span class="caps">QML</span> Elements] ''[qt.io]''.
# Create Qt application and the QDeclarative view<br />app = QApplication(sys.argv)<br />view = QDeclarativeView()
# Create an URL to the QML file<br />url = QUrl('view.qml')
# Set the QML file and show<br />view.setSource(url)<br />view.show()
# Enter Qt main loop<br />sys.exit(app.exec_())<br /></code>


===Categories:===
If you are already familiar with PySide and have followed our tutorials, much of this code is already familiar. The only novelties are that you must import QDeclarativeView and set the source of the QDeclarativeView object to the URL of your QML file. Then, as any Qt widget, you call '''QDeclarativeView.show()'''.


* [[:Category:LanguageBindings|LanguageBindings]]
'''Hint:''' If you are programming for desktop, you should consider adding '''view.setResizeMode(QDeclarativeView.SizeRootObjectToView)''' before showing the view. This will force the outer QML rectangle to resize along with the outer window.
** [[:Category:LanguageBindings::PySide|PySide]]

Revision as of 14:15, 23 February 2015


English 한국어 日本語

Your first application using PySide and QtQuick/QML

QML is a declarative language designed to describe the user interface of a program: both what it looks like, and how it behaves. In QML, a user interface is specified as a tree of objects with properties. In this tutorial we will show how you can make a simple Hello World application with PySide and QML.

A PySide/QML application consists, at least, of two different files - a file with the QML description of the user interface, and a python file which loads the qml file. To avoid problems for now, don't forget to save both files in the same folder.

Here is a simple QML file, called view.qml:

<br />import QtQuick 1.0

Rectangle {<br /> width: 200<br /> height: 200<br /> color: &quot;red&amp;quot;

Text {<br /> text: &quot;Hello World&amp;quot;<br /> anchors.centerIn: parent<br /> }<br />}<br />

We start by importing QtQuick 1.0, since in theory, QtQuick has a different release schedule than Qt/PySide.

The rest of the QML code is pretty straightforward for those who have previously used HTML or XML files. Basically, we are creating a red rectangle with the size 200*200 and, inside that rectangle, we are adding a Text element which says Hello World. The code anchors.centerIn: parent just makes the text appear centered in relation to its immediate parent.

Now, let's see how the code looks on the PySide. Let's call it main.py:

<br />#!/usr/bin/env python<br /># <s>'''- coding: utf-8 -'''</s>

import sys<br />from PySide.QtCore import *<br />from PySide.QtGui import *<br />from PySide.QtDeclarative import QDeclarativeView

# Create Qt application and the QDeclarative view<br />app = QApplication(sys.argv)<br />view = QDeclarativeView()
# Create an URL to the QML file<br />url = QUrl('view.qml')
# Set the QML file and show<br />view.setSource(url)<br />view.show()
# Enter Qt main loop<br />sys.exit(app.exec_())<br />

If you are already familiar with PySide and have followed our tutorials, much of this code is already familiar. The only novelties are that you must import QDeclarativeView and set the source of the QDeclarativeView object to the URL of your QML file. Then, as any Qt widget, you call QDeclarativeView.show().

Hint: If you are programming for desktop, you should consider adding view.setResizeMode(QDeclarativeView.SizeRootObjectToView) before showing the view. This will force the outer QML rectangle to resize along with the outer window.