qtqml.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // @snippet qmlregistersingletoninstance
  2. .. py:function:: qmlRegisterSingletonInstance(pytype: type,\
  3. uri: str,\
  4. versionMajor: int,\
  5. versionMinor: int,\
  6. typeName: str,\
  7. instanceObject: object) -> int
  8. :param type pytype: Python class
  9. :param str uri: uri to use while importing the component in QML
  10. :param int versionMajor: major version
  11. :param int versionMinor: minor version
  12. :param str typeName: name exposed to QML
  13. :param object instanceObject: singleton object to be registered
  14. :return: int (the QML type id)
  15. This function registers a singleton Python object *instanceObject*, with a
  16. particular *uri* and *typeName*. Its version is a combination of *versionMajor*
  17. and *versionMinor*. Use this function to register an object of the given type
  18. *pytype* as a singleton type.
  19. // @snippet qmlregistersingletoninstance
  20. // @snippet qmlregistersingletontype_qobject_nocallback
  21. .. py:function:: qmlRegisterSingletonType(pytype: type, uri: str, versionMajor: int, versionMinor: int, typeName: str) -> int
  22. :param type pytype: Python class
  23. :param str uri: uri to use while importing the component in QML
  24. :param int versionMajor: major version
  25. :param int versionMinor: minor version
  26. :param str typeName: name exposed to QML
  27. :return: int (the QML type id)
  28. This function registers a Python type as a singleton in the QML system.
  29. Alternatively, the :ref:`QmlSingleton` decorator can be used.
  30. // @snippet qmlregistersingletontype_qobject_nocallback
  31. // @snippet qmlregistersingletontype_qobject_callback
  32. .. py:function:: qmlRegisterSingletonType(pytype: type, uri: str, versionMajor: int, versionMinor: int, typeName: str, callback: object) -> int
  33. :param type pytype: Python class
  34. :param str uri: uri to use while importing the component in QML
  35. :param int versionMajor: major version
  36. :param int versionMinor: minor version
  37. :param str typeName: name exposed to QML
  38. :param object callback: Python callable (to handle Python type)
  39. :return: int (the QML type id)
  40. This function registers a Python type as a singleton in the QML system using
  41. the provided callback (which gets a QQmlEngine as a parameter) to generate the
  42. singleton.
  43. // @snippet qmlregistersingletontype_qobject_callback
  44. // @snippet qmlregistersingletontype_qjsvalue
  45. .. py:function:: qmlRegisterSingletonType(uri: str, versionMajor: int, versionMinor: int, typeName: str, callback: object) -> int
  46. :param str uri: uri to use while importing the component in QML
  47. :param int versionMajor: major version
  48. :param int versionMinor: minor version
  49. :param str typeName: name exposed to QML
  50. :param object callback: Python callable (to handle QJSValue)
  51. :return: int (the QML type id)
  52. This function registers a QJSValue as a singleton in the QML system using the
  53. provided callback (which gets a QQmlEngine as a parameter) to generate the
  54. singleton.
  55. // @snippet qmlregistersingletontype_qjsvalue
  56. // @snippet qmlregistertype
  57. .. py:function:: qmlRegisterType(pytype: type, uri: str, versionMajor: int, versionMinor: int, qmlName: str) -> int
  58. :param type pytype: Python class
  59. :param str uri: uri to use while importing the component in QML
  60. :param int versionMajor: major version
  61. :param int versionMinor: minor version
  62. :param str qmlName: name exposed to QML
  63. :return: int (the QML type id)
  64. This function registers the Python *type* in the QML system with the name
  65. *qmlName*, in the library imported from *uri* having the version number
  66. composed from *versionMajor* and *versionMinor*. For example, this registers a
  67. Python class 'MySliderItem' as a QML type named 'Slider' for version '1.0' of a
  68. module called 'com.mycompany.qmlcomponents':
  69. ::
  70. qmlRegisterType(MySliderItem, "com.mycompany.qmlcomponents", 1, 0, "Slider")
  71. Once this is registered, the type can be used in QML by importing the specified
  72. module name and version number:
  73. ::
  74. import com.mycompany.qmlcomponents 1.0
  75. Slider { ... }
  76. Note that it's perfectly reasonable for a library to register types to older
  77. versions than the actual version of the library. Indeed, it is normal for the
  78. new library to allow QML written to previous versions to continue to work, even
  79. if more advanced versions of some of its types are available.
  80. // @snippet qmlregistertype
  81. // @snippet qmlregisteruncreatabletype
  82. .. py:function:: qmlRegisterUncreatableType(pytype: type, uri: str, versionMajor: int, versionMinor: int, qmlName: str, noCreationReason: str) -> int
  83. :param type pytype: Python class
  84. :param str uri: uri to use while importing the component in QML
  85. :param int versionMajor: major version
  86. :param int versionMinor: minor version
  87. :param str qmlName: name exposed to QML
  88. :param str noCreationReason: Error message shown when trying to create the QML type
  89. :return: int (the QML type id)
  90. This function registers the Python *type* in the QML system as an uncreatable
  91. type with the name *qmlName*, in the library imported from *uri* having the
  92. version number composed from *versionMajor* and *versionMinor*, showing
  93. *noCreationReason* as an error message when creating the type is attempted. For
  94. example, this registers a Python class 'MySliderItem' as a QML type named
  95. 'Slider' for version '1.0' of a module called 'com.mycompany.qmlcomponents':
  96. ::
  97. qmlRegisterUncreatableType(MySliderItem, "com.mycompany.qmlcomponents", 1, 0, "Slider", "Slider cannot be created.")
  98. Note that it's perfectly reasonable for a library to register types to older
  99. versions than the actual version of the library. Indeed, it is normal for the
  100. new library to allow QML written to previous versions to continue to work, even
  101. if more advanced versions of some of its types are available.
  102. Alternatively, the :ref:`QmlUncreatable` decorator can be used.
  103. // @snippet qmlregisteruncreatabletype
  104. // @snippet qqmlengine-singletoninstance-qmltypeid
  105. Returns the instance of a singleton type that was registered under qmlTypeId.
  106. For ``QObject``-derived singleton types, the ``QObject`` instance is returned,
  107. otherwise a ``QJSValue`` or ``None``.
  108. It is recommended to store the QML type id, e.g. as a static member in the
  109. singleton class. The lookup via qmlTypeId() is costly.
  110. // @snippet qqmlengine-singletoninstance-qmltypeid
  111. // @snippet qqmlengine-singletoninstance-typename
  112. Returns the instance of a singleton type named typeName from the module specified
  113. by uri. For ``QObject``-derived singleton types, the ``QObject`` instance is
  114. returned, otherwise a ``QJSValue`` or ``None``.
  115. This method can be used as an alternative to calling qmlTypeId followed by the
  116. id based overload of singletonInstance. This is convenient when one only needs
  117. to do a one time setup of a singleton; if repeated access to the singleton is
  118. required, caching its typeId will allow faster subsequent access via the
  119. type-id based overload.
  120. // @snippet qqmlengine-singletoninstance-typename