Improved QML Support in Qt for Python 6.0

Since the initial port of PySide to Qt5 (a.k.a PySide2), the interaction with QML was on the list of features we wanted to fully support in our set of bindings, due to the popularity of QML.

With the first official release 5.12, we had cover many use cases for the QML and Python interaction, but also we left out a couple of use cases that were really required by our community. Now that we are developing new features and improvements for Qt6, we wanted to address most of them.

Here you can find a summary of the features that we have done so far for the planned Qt for Python 6.0 release.

Unlimited types

The previous limitation of registering a maximum of 50 custom types is gone with 6.0, you may now register as many types as you wish.

Singleton Types

Starting with 6.0, you can register custom singletons for use in QML with qmlRegisterSingletonType.

For example if you wanted to expose information about the the operating system to QML, you could create the following class:

class SystemInfo(QObject):
def __init__(self):
QObject.__init__(self)

def getOSName(self):
return platform.system()

def getOSRelease(self):
return platform.release()

osName = Property(str, getOSName)
osRelease = Property(str, getOSRelease)

And then register it under CustomType 1.0:

qmlRegisterSingletonType(SystemInfo, "CustomType", 1, 0, "SystemInfo")

Now you may access SystemInfo by simply importing CustomType in your QML code:

import CustomType 1.0

Text {
text: "OS Name: " + SystemInfo.osName
}

Uncreatable Types

Another missing feature was the option to register custom QML types as uncreatable, this will be possible with qmlRegisterUncreatableType

You may use it e.g. to register a custom enum in QML (which utilizes the new QEnum annotation):

class Theme(QObject):
@QEnum
class Variant(Enum):
Default, Dark, HighContrast = range(3)

# ... qmlRegisterUncreatableType(Theme, "CustomType", 1, 0, "Theme", "Theme can't be created")

After registering you can use the enum like this:

import QtQuick 2.0

import CustomType 1.0

Item {
property int theme: Theme.Default
}

Adapting to the new way of registering types

Qt for Python now also supports a QmlElement decorator that works in a similar way to QML_ELEMENT in C++.

QML_IMPORT_NAME = "com.library.name"
QML_IMPORT_MAJOR_VERSION = 1
QML_IMPORT_MINOR_VERSION = 0 # Optional
@QmlElement
class ClassForQml(QObject):
# ...

e.g. this Snippet will expose ClassForQml to QML and can be imported via the import name and version specified in global variables. This may be especially useful to you if you are registering a lot of types.

Conclusion

We understand that there might be other aspects of the QML + Python interaction that  your application currently needs, or will benefit from having it, so we encourage you to share with us missing features, issues, or even new ideas on how we can make Qt for Python a really good alternative for QML-based applications.

Check out our JIRA system, where you can share the ideas or contact us in our many community platforms.


Blog Topics:

Comments