Changes to Qt QML

Qt 6 是努力使框架更高效,且更易于使用的结果。

为兼容每个发行的所有公共 API,我们试着维护二进制和源代码。但是,为使 Qt 成为更优框架,一些改变是不可避免的。

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 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.

  • 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);
    					
  • 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);
    		

版权所有  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1