Filling-and-reading-QML-UI-forms-from-Python: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Filling and reading <span class="caps">QML</span> UI forms from Python=
[[Category:LanguageBindings::PySide]]<br />[[Category:snippets]]<br />[[Category:Developing_with_Qt::Qt Quick]]<br />[[Category:Developing_with_Qt::Qt Quick::QML]]<br />[[Category:Developing_with_Qt::Qt Quick::Tutorial]]


This [[PySide]] tutorial shows you how to create a “classic” form-based UI with the Colibri <span class="caps">QML</span> Components and have it filled and controlled by Python code. There are several ways to do this, and depending on your use case, there might be a better method. Please also note that in this example, the controller code knows a bit about the UI (or rather: the UI has to inform the controller which widgets are to be filled), which might not be desired.
= Filling and reading QML UI forms from Python =


==CarAnalogy.py==
This [[PySide]] tutorial shows you how to create a &quot;classic&amp;quot; form-based UI with the Colibri QML Components and have it filled and controlled by Python code. There are several ways to do this, and depending on your use case, there might be a better method. Please also note that in this example, the controller code knows a bit about the UI (or rather: the UI has to inform the controller which widgets are to be filled), which might not be desired.


===Import the required modules===
== CarAnalogy.py ==


We need the '''QtCore''' module for QObject, the '''QtGui''' module for QApplication and the '''QtDeclarative''' module for the <span class="caps">QML</span> View (QDeclarativeView):
=== Import the required modules ===


===Define a Car as QObject===
We need the '''QtCore''' module for QObject, the '''QtGui''' module for QApplication and the '''QtDeclarative''' module for the QML View (QDeclarativeView):
 
<code><br />import sys
 
from PySide import QtCore, QtGui, QtDeclarative<br /></code>
 
=== Define a Car as QObject ===


This is simply the Python version of a normal QObject with 4 properties:
This is simply the Python version of a normal QObject with 4 properties:


* '''model''' (String) The car name
* '''model''' (String) - The car name
* '''brand''' (String) The company who made the card
* '''brand''' (String) - The company who made the card
* '''year''' (int) The year it was first produced
* '''year''' (int) - The year it was first produced
* '''inStock''' (bool) If the car is still in stock at the warehouse
* '''inStock''' (bool) - If the car is still in stock at the warehouse
 
<code><br />class Car(QtCore.QObject):<br /> def ''init''(self, model='', brand='', year=0, in_stock=False):<br /> QtCore.QObject.''init''(self)<br /> self._''model = model<br /> self.brand = brand<br /> self.year = year<br /> self.''_in_stock = in_stock
 
changed = QtCore.Signal()


===The controller to fill the form and react to events===
def ''model(self): return self.''_model<br /> def ''brand(self): return self.''_brand<br /> def ''year(self): return self.''_year<br /> def ''inStock(self): return self.''_in_stock


This is another QObject that takes a list of cars as constructor parameter. It also remembers the current position in the list of cars. There are three slots that are visible to the “outside” (<span class="caps">QML</span> in our case):
def ''setModel(self, model):<br /> self.''_model = model<br /> self.changed.emit()


* '''prev''' – Go to the previous item
def ''setBrand(self, brand):<br /> self.''_brand = brand<br /> self.changed.emit()
* '''next''' – Go to the next item
* '''init''' – Show the first item


All these slots take a QObject as parameter, and from the <span class="caps">QML</span> file, we will pass the root object there, which has a property '''widgets''' where we save a dictionary of mappings from name to <span class="caps">QML</span> component. The '''fill''' function takes care of filling in the data of the current car into the <span class="caps">QML</span> widgets.
def ''setYear(self, year):<br /> self.''_year = year<br /> self.changed.emit()


===Example data===
def ''setInStock(self, in_stock):<br /> self.''_in_stock = in_stock<br /> self.changed.emit()


Here is some example data, so that we can use our example and click through a list of cars:
model = QtCore.Property(str, _model, _setModel, notify=changed)<br /> brand = QtCore.Property(str, _brand, _setBrand, notify=changed)<br /> year = QtCore.Property(int, _year, _setYear, notify=changed)<br /> inStock = QtCore.Property(bool, _inStock, ''setInStock, notify=changed)<br /></code>
<br />h3. The controller to fill the form and react to events
<br />This is another QObject that takes a list of cars as constructor parameter. It also remembers the current position in the list of cars. There are three slots that are visible to the &quot;outside&amp;quot; (QML in our case):
<br />* '''prev''' - Go to the previous item<br />* '''next''' - Go to the next item<br />* '''init''' - Show the first item
<br />All these slots take a QObject as parameter, and from the QML file, we will pass the root object there, which has a property '''widgets''' where we save a dictionary of mappings from name to QML component. The '''fill''' function takes care of filling in the data of the current car into the QML widgets.
<br /><code><br />class Controller(QtCore.QObject):<br /> definit<span class="lst self,">:<br /> QtCore.QObject.</span>init''_(self)<br /> self._lst = lst<br /> self._pos = 0


===Putting it all together===
def fill(self, widgets):<br /> widgets['model'].setProperty('text', self._lst[self._pos].model)<br /> widgets['brand'].setProperty('text', self._lst[self._pos].brand)<br /> widgets['year'].setProperty('value', self._lst[self._pos].year)<br /> widgets['inStock'].setProperty('checked', self._lst[self._pos].inStock)<br /> widgets['position'].setProperty('text', 'd/%d' (self._pos+1, len(self._lst)))


We first need to create the '''controller''', which then also knows about our '''cars'''. Then, there is some housekeeping that we need to do – create a QApplication, create the QDeclarativeView and set its resizing mode (so that the root object in <span class="caps">QML</span> is always as big as the window).
</code>QtCore.Slot(QtCore.QObject)<br /> def prev(self, root):<br /> print 'prev'<br /> self._pos = max(0, self._pos - 1)<br /> self.fill(root.property('widgets'))


We then get the root context and expose the controller and the cars list to it (if you look closely, we don’t really need the cars themselves). Then, we load the <span class="caps">QML</span> file, show the view and start the application.
<code>QtCore.Slot(QtCore.QObject)<br /> def next(self, root):<br /> print 'next'<br /> self._pos = min(len(self._lst) - 1, self.''pos + 1)<br /> self.fill(root.property('widgets'))
<br /> </code>QtCore.Slot(QtCore.QObject)<br /> def init(self, root):<br /> print 'init'<br /> self.fill(root.property('widgets'))<br /><code>
<br />h3. Example data
<br />Here is some example data, so that we can use our example and click through a list of cars:
<br /></code><br />cars = [<br /> Car('Model T', 'Ford', 1908),<br /> Car('Beetle', 'Volkswagen', 1938, True),<br /> Car('Corolla', 'Toyota', 1966),<br /> Car('Clio', 'Renault', 1991, True),<br /> Car('Ambassador', 'Hindustan', 1958),<br /> Car('Uno', 'Fiat', 1983, True),<br /> Car('Ibiza', 'Seat', 1984, True),<br />]<br /><code>
<br />h3. Putting it all together
<br />We first need to create the '''controller''', which then also knows about our '''cars'''. Then, there is some housekeeping that we need to do - create a QApplication, create the QDeclarativeView and set its resizing mode (so that the root object in QML is always as big as the window).
<br />We then get the root context and expose the controller and the cars list to it (if you look closely, we don't really need the cars themselves). Then, we load the QML file, show the view and start the application.
<br /></code><br />controller = Controller(cars)
<br />app = QtGui.QApplication(sys.argv)
<br />view = QtDeclarative.QDeclarativeView()<br />view.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView)
<br />ctx = view.rootContext()
<br />for name in ('controller', 'cars'):<br /> ctx.setContextProperty(name, locals()[name])
<br />view.setSource(file.replace('.py', '.qml'))<br />view.show()
<br />app.exec''()<br /><code>


==CarAnalogy.qml==
== CarAnalogy.qml ==


This is the user interface of our application. We only use the controller in the UI, and we also only use it for initialization and when buttons are clicked.
This is the user interface of our application. We only use the controller in the UI, and we also only use it for initialization and when buttons are clicked.


==How the example app looks like==
</code>
 
import Qt 4.7<br />import &quot;colibri&amp;quot;


Simply start the resulting app with '''python CarAnalogy.py''' and you should get something like this:
Rectangle {<br /> id: page
 
property variant widgets
 
width: 800<br /> height: 480
 
Grid {<br /> id: grid<br /> columns: 2<br /> anchors.centerIn: parent<br /> spacing: 10
 
Row {<br /> CLButton { text: &quot;←&amp;quot;; onClicked: { controller.prev(page) } }<br /> CLButton { text: &quot;→&amp;quot;; onClicked: { controller.next(page) } }<br /> }
 
Text { id: position; text: &quot; &quot; }
 
Text { text: &quot;Model:&quot; }
 
CLLineEdit { id: model }
 
Text { text: &quot;Brand:&quot; }
 
CLLineEdit { id: brand }
 
Text { text: &quot;Year:&quot; }
 
Column {<br /> spacing: 10<br /> CLSlider {<br /> id: year<br /> minimum: 1900<br /> maximum: 2010<br /> }<br /> Text {<br /> text: year.value<br /> }<br /> }


[[Image:5229664866_f08aba1e4d_z.jpg|Screenshot of the Car Analogy example]]
Text { text: &quot; &quot; }


===Categories:===
Row {<br /> spacing: 10<br /> CLCheckBox { id: inStock }<br /> Text { text: &quot;In Stock&amp;quot; }<br /> }<br /> }


* [[:Category:Developing with Qt|Developing_with_Qt]]
Component.onCompleted: {<br /> widgets = {<br /> 'position': position,<br /> 'model': model,<br /> 'brand': brand,<br /> 'year': year,<br /> 'inStock': inStock,<br /> }<br /> controller.init(page)<br /> }<br />}<br /><code>
** [[:Category:Developing with Qt::Qt-Quick|Qt Quick]]
* [[:Category:Developing with Qt::Qt-Quick::QML|QML]]


* [[:Category:Developing with Qt::Qt-Quick::Tutorial|Tutorial]]
== How the example app looks like ==


* [[:Category:LanguageBindings|LanguageBindings]]
Simply start the resulting app with '''python CarAnalogy.py''' and you should get something like this:
** [[:Category:LanguageBindings::PySide|PySide]]
* [[:Category:snippets|snippets]]

Revision as of 08:56, 24 February 2015





Filling and reading QML UI forms from Python

This PySide tutorial shows you how to create a "classic&quot; form-based UI with the Colibri QML Components and have it filled and controlled by Python code. There are several ways to do this, and depending on your use case, there might be a better method. Please also note that in this example, the controller code knows a bit about the UI (or rather: the UI has to inform the controller which widgets are to be filled), which might not be desired.

CarAnalogy.py

Import the required modules

We need the QtCore module for QObject, the QtGui module for QApplication and the QtDeclarative module for the QML View (QDeclarativeView):

<br />import sys

from PySide import QtCore, QtGui, QtDeclarative<br />

Define a Car as QObject

This is simply the Python version of a normal QObject with 4 properties:

  • model (String) - The car name
  • brand (String) - The company who made the card
  • year (int) - The year it was first produced
  • inStock (bool) - If the car is still in stock at the warehouse
<br />class Car(QtCore.QObject):<br /> def ''init''(self, model='', brand='', year=0, in_stock=False):<br /> QtCore.QObject.''init''(self)<br /> self._''model = model<br /> self.brand = brand<br /> self.year = year<br /> self.''_in_stock = in_stock

changed = QtCore.Signal()

def ''model(self): return self.''_model<br /> def ''brand(self): return self.''_brand<br /> def ''year(self): return self.''_year<br /> def ''inStock(self): return self.''_in_stock

def ''setModel(self, model):<br /> self.''_model = model<br /> self.changed.emit()

def ''setBrand(self, brand):<br /> self.''_brand = brand<br /> self.changed.emit()

def ''setYear(self, year):<br /> self.''_year = year<br /> self.changed.emit()

def ''setInStock(self, in_stock):<br /> self.''_in_stock = in_stock<br /> self.changed.emit()

model = QtCore.Property(str, _model, _setModel, notify=changed)<br /> brand = QtCore.Property(str, _brand, _setBrand, notify=changed)<br /> year = QtCore.Property(int, _year, _setYear, notify=changed)<br /> inStock = QtCore.Property(bool, _inStock, ''setInStock, notify=changed)<br />


h3. The controller to fill the form and react to events
This is another QObject that takes a list of cars as constructor parameter. It also remembers the current position in the list of cars. There are three slots that are visible to the "outside&quot; (QML in our case):
* prev - Go to the previous item
* next - Go to the next item
* init - Show the first item
All these slots take a QObject as parameter, and from the QML file, we will pass the root object there, which has a property widgets where we save a dictionary of mappings from name to QML component. The fill function takes care of filling in the data of the current car into the QML widgets.


<br />class Controller(QtCore.QObject):<br /> definit<span class="lst self,">:<br /> QtCore.QObject.</span>init''_(self)<br /> self._lst = lst<br /> self._pos = 0

def fill(self, widgets):<br /> widgets['model'].setProperty('text', self._lst[self._pos].model)<br /> widgets['brand'].setProperty('text', self._lst[self._pos].brand)<br /> widgets['year'].setProperty('value', self._lst[self._pos].year)<br /> widgets['inStock'].setProperty('checked', self._lst[self._pos].inStock)<br /> widgets['position'].setProperty('text', 'd/%d' (self._pos+1, len(self._lst)))

QtCore.Slot(QtCore.QObject)
def prev(self, root):
print 'prev'
self._pos = max(0, self._pos - 1)
self.fill(root.property('widgets'))

QtCore.Slot(QtCore.QObject)<br /> def next(self, root):<br /> print 'next'<br /> self._pos = min(len(self._lst) - 1, self.''pos + 1)<br /> self.fill(root.property('widgets'))
<br />

QtCore.Slot(QtCore.QObject)
def init(self, root):
print 'init'
self.fill(root.property('widgets'))

<br />h3. Example data
<br />Here is some example data, so that we can use our example and click through a list of cars:
<br />


cars = [
Car('Model T', 'Ford', 1908),
Car('Beetle', 'Volkswagen', 1938, True),
Car('Corolla', 'Toyota', 1966),
Car('Clio', 'Renault', 1991, True),
Car('Ambassador', 'Hindustan', 1958),
Car('Uno', 'Fiat', 1983, True),
Car('Ibiza', 'Seat', 1984, True),
]

<br />h3. Putting it all together
<br />We first need to create the '''controller''', which then also knows about our '''cars'''. Then, there is some housekeeping that we need to do - create a QApplication, create the QDeclarativeView and set its resizing mode (so that the root object in QML is always as big as the window).
<br />We then get the root context and expose the controller and the cars list to it (if you look closely, we don't really need the cars themselves). Then, we load the QML file, show the view and start the application.
<br />


controller = Controller(cars)


app = QtGui.QApplication(sys.argv)
view = QtDeclarative.QDeclarativeView()
view.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView)
ctx = view.rootContext()
for name in ('controller', 'cars'):
ctx.setContextProperty(name, locals()[name])
view.setSource(file.replace('.py', '.qml'))
view.show()


app.exec()

== CarAnalogy.qml ==

This is the user interface of our application. We only use the controller in the UI, and we also only use it for initialization and when buttons are clicked.

import Qt 4.7
import "colibri&quot;

Rectangle {
id: page

property variant widgets

width: 800
height: 480

Grid {
id: grid
columns: 2
anchors.centerIn: parent
spacing: 10

Row {
CLButton { text: "←&quot;; onClicked: { controller.prev(page) } }
CLButton { text: "→&quot;; onClicked: { controller.next(page) } }
}

Text { id: position; text: " " }

Text { text: "Model:" }

CLLineEdit { id: model }

Text { text: "Brand:" }

CLLineEdit { id: brand }

Text { text: "Year:" }

Column {
spacing: 10
CLSlider {
id: year
minimum: 1900
maximum: 2010
}
Text {
text: year.value
}
}

Text { text: " " }

Row {
spacing: 10
CLCheckBox { id: inStock }
Text { text: "In Stock&quot; }
}
}

Component.onCompleted: {
widgets = {
'position': position,
'model': model,
'brand': brand,
'year': year,
'inStock': inStock,
}
controller.init(page)
}
}

How the example app looks like

Simply start the resulting app with python CarAnalogy.py and you should get something like this: