Qt for Python Tutorial HelloQML: Difference between revisions
AutoSpider (talk | contribs) (Rename category "LanguageBindings::PySide" -> "PySide") |
m (replaced 'code' tags with 'pre' tags) |
||
Line 12: | Line 12: | ||
Here is a simple QML file, called '''view.qml''': | Here is a simple QML file, called '''view.qml''': | ||
< | <pre> | ||
import QtQuick 1.0 | import QtQuick 1.0 | ||
Line 25: | Line 25: | ||
} | } | ||
} | } | ||
</ | </pre> | ||
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. | ||
Line 33: | Line 33: | ||
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''': | ||
< | <pre> | ||
#!/usr/bin/env python | #!/usr/bin/env python | ||
# -'''- coding: utf-8 -'''- | # -'''- coding: utf-8 -'''- | ||
Line 52: | Line 52: | ||
# Enter Qt main loop | # Enter Qt main loop | ||
sys.exit(app.exec_()) | sys.exit(app.exec_()) | ||
</ | </pre> | ||
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()'''. | 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. | '''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. |
Revision as of 22:11, 15 April 2017
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:
import QtQuick 1.0 Rectangle { width: 200 height: 200 color: "red" Text { text: "Hello World" anchors.centerIn: parent } }
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:
#!/usr/bin/env python # -'''- coding: utf-8 -'''- import sys from PySide.QtCore import * from PySide.QtGui import * from PySide.QtDeclarative import QDeclarativeView # Create Qt application and the QDeclarative view app = QApplication(sys.argv) view = QDeclarativeView() # Create an URL to the QML file url = QUrl('view.qml') # Set the QML file and show view.setSource(url) view.show() # Enter Qt main loop sys.exit(app.exec_())
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.