Changes to Qt QML

Qt 6 is a result of the conscious effort to make the framework more efficient and easy to use.

We try to maintain binary and source compatibility for all the public APIs in each release. But some changes were inevitable in an effort to make Qt a better framework.

In this topic we summarize those changes in Qt QML, and provide guidance to handle them.

QML 語言

URL resolution

In Qt 5, relative urls were often, albeit inconsistently, directly resolved, especially when assigned to an url property. In Qt 6 this is no longer the case.

If you had a QML file stored under "/home/qml/example.qml", and "example.qml" contained

property url imageFolder: "./images"
					

then the url property would store the URL "/home/qml/images". This made it impossible to use relative URLs in QML in this way, so in Qt 6, the URL stays relative, and only gets resolved when this is required (e.g. when it is used as the source of an Image component). If you depend on the old behavior, you can use Qt.resolvedUrl

例如,若有代碼像

property url imageFolder: "./images"
					

可以把它重寫成

property url imageFolder: Qt.resolvedUrl("./images")
					

Qt.resolvedUrl can be used in both Qt 5 and 6.

Variant Properties

variant properties, which have been marked as obsolete since Qt 5, are now treated in exactly the same way as var properties. Code that relied on implicit string conversion triggered on assignment to variant properties should be updated to explicitly create an object of the correct type.

例如,若有代碼像

property variant myColor: "red"
					

可以把它重寫成

property variant myColor: Qt.color("red")
					

Implicit conversions were done for strings that could be parsed as

  • color (use Qt.color instead),
  • date (use the Date object instead),
  • rect (use Qt.rect instead) and
  • size (use Qt.size instead)

variant still remains a deprecated keyword in Qt 6, though new code is strongly encouraged to use var properties instead.

注意: If the type of the property is known not to change, use a property of the concrete type, instead of a var 特性。

注意: These conversions were also applied to QVariant properties of classes registered with the engine. As with variant properties, code that relied on implicit string conversions need to use the corresponding functions of the Qt object.

Source Incompatible API Changes

Changed API

QQmlListProperty 's CountFunction and AtFunction have been changed to use qsizetype 而不是 int to align with the corresponding changes in Qt's containers.

例如,若有代碼像

int myCountFunction(QQmlListProperty<MyType> *);
MyType *myAtFunction(QQmlListProperty<MyType> *, int);
QQmlListProperty<MyType> myReadOnlyList(containingObject, container, &myCountFunction,
                                        &myAtFunction);
					

可以把它重寫成

qsizetype myCountFunction(QQmlListProperty<MyType> *);
MyType *myAtFunction(QQmlListProperty<MyType> *, qsizetype);
QQmlListProperty<MyType> myReadOnlyList(containingObject, container, &myCountFunction,
                                        &myAtFunction);
					

Code which needs to supports both Qt 5 and Qt 6 can either use a typedef which is int in Qt 5 and qsizetype in Qt 6, or use QList::size_type , which already is such a type alias.

Removed API

Various deprecated functions have been removed.

  • The QQmlListProperty constructor taking a reference has been removed.

    例如,若有代碼像

    QQmlListProperty<QObject>(owner, owner->objectList);
    						

    可以把它重寫成

    QQmlListProperty<QObject>(owner, &owner->objectList);
    						
  • 函數 qmlDebug , qmlInfo , qmlWarning , qmlContext and qmlEngine used to exist both in the global namespace (or Qt namespace in namespaced builds), and in the QtQml namespace. These functions now exist only in the global namespace.

    例如,若有代碼像

    QQmlEngine *engine = QtQml::qmlEngine(qmlObject);
    					

    可以把它重寫成

    QQmlEngine *engine = qmlEngine(qmlObject);
    					
  • The qmlRegisterType overload taking no arguments has been removed. Use qmlRegisterAnonymousType instead, or switch to declarative type registration with QML_ANONYMOUS .

    例如,若有代碼像

    class AnonymousType : public QObject {
      // ...
    };
    qmlRegisterType<AnonymousType>();
    				

    可以把它重寫成

    class AnonymousType : public QObject {
      // ...
    };
    qmlRegisterAnonymousType<AnonymousType>("MyModule", 1);
    				

    Or alternatively

    class AnonymousType : public QObject {
        QML_ANONYMOUS
        // ...
    };
    				
  • The overloads of qmlRegisterExtendedType and qmlRegisterInterface which take no version argument have been removed. Use the overloads providing a version, or switch to declarative type registration with QML_EXTENDED and QML_INTERFACE .

    例如,若有代碼像

    struct GreetInterface
    {
       virtual ~GreetInterface();
       virtual void greet();
    };
    Q_DECLARE_INTERFACE(GreetInterface, "org.hi.GreetInterface")
    qmlRegisterInterface<GreetInterface>("Greeter");
    			

    可以把它重寫成

    struct GreetInterface
    {
       virtual ~GreetInterface();
       virtual void greet();
    };
    Q_DECLARE_INTERFACE(GreetInterface, "org.hi.GreetInterface")
    qmlRegisterInterface<GreetInterface>("Greeter", 1);
    			

    Alternatively

    struct GreetInterface
    {
       QML_INTERFACE(Greeter)
       virtual ~GreetInterface();
       virtual void greet();
    };
    Q_DECLARE_INTERFACE(GreetInterface, "org.hi.GreetInterface")
    			

    注意: In new code, declarative type registration should be preferred.

  • 函數 QJSValue::engine has been removed. If access to the engine is required, a reference to it must be stored instead.
  • qmlAttachedPropertiesObjectById and qmlAttachedPropertiesObject(int *, const QObject *, const QMetaObject *, bool) have been removed. Use the qmlAttachedPropertiesObject(QObject *, QQmlAttachedPropertiesFunc, bool) overload of qmlAttachedPropertiesObject 代替。
  • QJSEngine::installTranslatorFunctions has been removed. QJSEngine::installExtensions is available as a replacement.

    例如,若有代碼像

    QJSEngine engine;
    engine.installTranslatorFunctions();
    		

    可以把它重寫成

    QJSEngine engine;
    engine.installExtensions(QJSEngine::TranslationExtension);
    		

內容

  1. QML 語言

  2. URL resolution

  3. Variant Properties

  4. Source Incompatible API Changes

  5. Changed API

  6. Removed API

  7. 版權所有  © 2014-2026 樂數軟件    

    工業和信息化部: 粵ICP備14079481號-1