×
Namespaces

Variants
Actions

Python Harness for QML

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Compatibility
Platform(s):
Symbian
Article
Created: rmerren (30 Nov 2010)
Last edited: hamishwillee (24 Jul 2012)

Sure you can write complete programs in QML, but how do you run them? You can always run them with the qmlviewer application for testing, but if you want something without the qmlviewer menus (and without having to crack open a C++ compiler) you can harness the qml in Python. There are two options for binding Python with Qt: PyQt4 and PySide. In this example we are using PySide[1]. (Note: You will need to have Python and Pyside and Qt all correctly installed for this to work--installation may vary depending on your system.)

If you have a QML file called MyQmlApp.qml and you want the window title to be My QML Application, then save the following code in a file called MyQmlApp.py:

#!/usr/bin/env python
 
# If QML_FILE_NAME is None, the QML file must
# have the same name as this Python script, just with
# .qml as extension instead of .py. You can override
# this by setting an explicit filename here.
QML_FILE_NAME = None
WINDOW_TITLE = "My QML Application"
 
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import *
import sys
if __name__ == '__main__':
app = QApplication(sys.argv)
f = QDeclarativeView()
f.setSource(QML_FILE_NAME or __file__.replace('.py', '.qml'))
f.setWindowTitle(WINDOW_TITLE)
f.show()
app.exec_()

You can change the QML_FILE_NAME and WINDOW_TITLE values (and the file name, of course) to whatever you like. Executing the MyQmlApp.py file with Python will show your QML application in a clean window with no extraneous menus and with your chosen window title.

Alternative: EasyQML

This module provides even more abstraction and allows you to set context properties easily by subclassing easyqml.App:

easyqml.py

import sys
import os
 
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import *
 
def run(index_qml, vars={}, title=None):
app = QApplication(sys.argv)
 
view = QDeclarativeView()
 
rc = view.rootContext()
for key, value in vars.items():
rc.setContextProperty(key, value)
 
view.setWindowTitle(title or os.path.basename(index_qml))
view.setSource(index_qml)
view.setResizeMode(QDeclarativeView.SizeRootObjectToView)
view.show()
 
app.exec_()
 
class App(object):
@classmethod
def run(cls, filename=None):
if filename is None:
filename = cls.__name__ + '.qml'
run(filename, cls.__dict__, cls.__doc__ or cls.__name__)

Usage

hiwelt.py

import easyqml
 
class hiwelt(easyqml.App):
greeting = 'Hello World'
 
hiwelt.run()

hiwelt.qml

import Qt 4.7
 
Rectangle {
width: 100
height: 200
Text {
text: greeting
anchors.centerIn: parent
}
}
This page was last modified on 24 July 2012, at 05:04.
75 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×