Qt v4.7 introduced QML as a means of declaratively describing a user interface and using JavaScript as a scripting language within QML. It is possible to write complete standalone QML applications, or to combine them with C++.
PyQt supports the integration of Python and QML as far as it can - given the limitations of the QML implementation.
There are three types of QML application:
QML can implement JavaScript functions and define signals. PyQt allows both of these to be accessed transparently from Python. The following is a simple example application that displays the current date and time when the user clicks on it.
The following is the QML (in the message.qml) that defines the user interface:
import Qt 4.7
Rectangle {
signal messageRequired;
function updateMessage(text) {
messageText.text = text
}
anchors.fill: parent; color: "black"
Text {
id: messageText
anchors.centerIn: parent; color: "white"
}
MouseArea {
anchors.fill: parent
onClicked: messageRequired()
}
}
This defines a signal messageRequired which is emitted when the user clicks on the mouse area. It also implements a function updateMessage() which updates the message being displayed.
The following is the Python code that implements the application logic:
import sys
from PyQt4.QtCore import QDateTime, QObject, QUrl, pyqtSignal
from PyQt4.QtGui import QApplication
from PyQt4.QtDeclarative import QDeclarativeView
# This class will emit the current date and time as a signal when
# requested.
class Now(QObject):
now = pyqtSignal(str)
def emit_now(self):
formatted_date = QDateTime.currentDateTime().toString()
self.now.emit(formatted_date)
app = QApplication(sys.argv)
now = Now()
# Create the QML user interface.
view = QDeclarativeView()
view.setSource(QUrl('message.qml'))
view.setResizeMode(QDeclarativeView.SizeRootObjectToView)
# Get the root object of the user interface. It defines a
# 'messageRequired' signal and JavaScript 'updateMessage' function. Both
# can be accessed transparently from Python.
rootObject = view.rootObject()
# Provide the current date and time when requested by the user interface.
rootObject.messageRequired.connect(now.emit_now)
# Update the user interface with the current date and time.
now.now.connect(rootObject.updateMessage)
# Provide an initial message as a prompt.
rootObject.updateMessage("Click to get the current date and time")
# Display the user interface and allow the user to interact with it.
view.setGeometry(100, 100, 400, 240)
view.show()
app.exec_()
Hopefully the comments in the code are sufficient explanation. The important things to note are that the signal and JavaScript function appear as appropriate attributes of the root object QDeclarativeItem returned by the rootObject() method of QDeclarativeView and can be connected to and called respectively.