Skip to content

qml

Class info

Classes

Name Children Inherits
JSEngine
prettyqt.qml.jsengine
Environment for evaluating JavaScript code.
    JSEngineMixin
    prettyqt.qml.jsengine
    JSValue
    prettyqt.qml.jsvalue
    Acts as a container for Qt/JavaScript data types.
      JSValueIterator
      prettyqt.qml.jsvalueiterator
      Java-style iterator for JSValue.
        QmlApplicationEngine
        prettyqt.qml.qmlapplicationengine
        Convenient way to load an application from a single QML file.
          QmlComponent
          prettyqt.qml.qmlcomponent
          Encapsulates a QML component definition.
            QmlEngine
            prettyqt.qml.qmlengine
            Environment for instantiating QML components.
              QmlEngineMixin
              prettyqt.qml.qmlengine
              QmlError
              prettyqt.qml.qmlerror
              Encapsulates a QML error.
                QmlExpression
                prettyqt.qml.qmlexpression
                Evaluates JavaScript in a QML context.
                  QmlImageProviderBase
                  prettyqt.qml.qmlimageproviderbase
                  Used to register image providers in the QML engine.
                    QmlImageProviderBaseMixin
                    prettyqt.qml.qmlimageproviderbase
                    QmlParserStatus
                    prettyqt.qml.qmlparserstatus
                    Updates on the QML parser state.
                      QmlParserStatusMixin
                      prettyqt.qml.qmlparserstatus
                      QmlProperty
                      prettyqt.qml.qmlproperty
                      Abstracts accessing properties on objects created from QML.
                        QmlPropertyMap
                        prettyqt.qml.qmlpropertymap
                        Allows you to set key-value pairs that can be used in QML bindings.
                          QmlPropertyMapMixin
                          prettyqt.qml.qmlpropertymap

                          🛈 DocStrings

                          Classes for QML and JavaScript languages.

                          JSEngine

                          Bases: JSEngineMixin, QJSEngine

                          Environment for evaluating JavaScript code.

                          Source code in prettyqt/qml/jsengine.py
                          class JSEngine(JSEngineMixin, qml.QJSEngine):
                              """Environment for evaluating JavaScript code."""
                          

                          JSValue

                          Bases: QJSValue

                          Acts as a container for Qt/JavaScript data types.

                          Source code in prettyqt/qml/jsvalue.py
                          class JSValue(qml.QJSValue):
                              """Acts as a container for Qt/JavaScript data types."""
                          
                              def __repr__(self):
                                  return get_repr(self, self.toVariant())
                          
                              def __len__(self):
                                  return self.property("length").toVariant()
                          
                              def __getitem__(self, index: int | str):
                                  return self.property(index).toVariant()
                          
                              def __delitem__(self, index: str):
                                  self.deleteProperty(index)
                          
                              def __setitem__(self, index: int | str, value):
                                  self.setProperty(index, value)
                          
                              def __iter__(self):
                                  iterator = qml.JSValueIterator(self)
                                  return iter(list(iterator))
                          
                              def __contains__(self, index: str):
                                  return self.hasProperty(index)
                          
                              def __call__(self, *args) -> JSValue:
                                  result = self.call(args)
                                  return JSValue(result)
                          
                              def get_value(self):
                                  return self.toVariant()
                          
                              def get_error_type(self) -> ErrorTypeStr | None:
                                  if (error_type := self.errorType()) == qml.QJSValue.ErrorType(0):
                                      return None
                                  return ERROR_TYPES.inverse[error_type]
                          
                              @classmethod
                              def from_object(cls, obj, jsengine: qml.QJSEngine) -> Self:
                                  """Convert any python object into a QJSValue (must happen in GUI thread)."""
                                  match obj:
                                      case None:
                                          return cls()
                                      case list() | tuple():
                                          length = len(obj)
                                          array = cls(jsengine.newArray(length))
                                          for i, v in enumerate(obj):
                                              array.setProperty(i, cls.from_object(v, jsengine))
                                          return array
                                      case dict():
                                          js_obj = cls(jsengine.newObject())
                                          for k, v in obj.items():
                                              js_obj.setProperty(k, cls.from_object(v, jsengine))
                                          return js_obj
                                      case _:
                                          try:
                                              return cls(obj)
                                          except TypeError:
                                              logger.debug("unknown type: %s", obj)
                                              return cls()
                          

                          from_object(obj, jsengine: qml.QJSEngine) -> Self classmethod

                          Convert any python object into a QJSValue (must happen in GUI thread).

                          Source code in prettyqt/qml/jsvalue.py
                          @classmethod
                          def from_object(cls, obj, jsengine: qml.QJSEngine) -> Self:
                              """Convert any python object into a QJSValue (must happen in GUI thread)."""
                              match obj:
                                  case None:
                                      return cls()
                                  case list() | tuple():
                                      length = len(obj)
                                      array = cls(jsengine.newArray(length))
                                      for i, v in enumerate(obj):
                                          array.setProperty(i, cls.from_object(v, jsengine))
                                      return array
                                  case dict():
                                      js_obj = cls(jsengine.newObject())
                                      for k, v in obj.items():
                                          js_obj.setProperty(k, cls.from_object(v, jsengine))
                                      return js_obj
                                  case _:
                                      try:
                                          return cls(obj)
                                      except TypeError:
                                          logger.debug("unknown type: %s", obj)
                                          return cls()
                          

                          JSValueIterator

                          Bases: QJSValueIterator

                          Java-style iterator for JSValue.

                          Source code in prettyqt/qml/jsvalueiterator.py
                          class JSValueIterator(QtQml.QJSValueIterator):
                              """Java-style iterator for JSValue."""
                          
                              def __iter__(self):
                                  return self
                          
                              def __next__(self):
                                  if self.next():
                                      return (self.name(), self.value().toVariant())
                                  raise StopIteration
                          

                          QmlApplicationEngine

                          Bases: QmlEngineMixin, QQmlApplicationEngine

                          Convenient way to load an application from a single QML file.

                          Source code in prettyqt/qml/qmlapplicationengine.py
                          class QmlApplicationEngine(qml.QmlEngineMixin, qml.QQmlApplicationEngine):
                              """Convenient way to load an application from a single QML file."""
                          
                              def __iter__(self) -> Iterator[QtCore.QObject]:
                                  return iter(self.rootObjects())
                          
                              def load_data(
                                  self,
                                  data: datatypes.ByteArrayType,
                                  url: datatypes.UrlType | os.PathLike[str] | None = None,
                              ):
                                  data = datatypes.to_bytearray(data)
                                  url = datatypes.to_local_url(url)
                                  self.loadData(data, url)
                          
                              def load_file(self, file: datatypes.UrlType | datatypes.PathType):
                                  if isinstance(file, os.PathLike):
                                      file = os.fspath(file)
                                  self.load(file)
                          

                          QmlComponent

                          Bases: ObjectMixin, QQmlComponent

                          Encapsulates a QML component definition.

                          Source code in prettyqt/qml/qmlcomponent.py
                          class QmlComponent(core.ObjectMixin, QtQml.QQmlComponent):
                              """Encapsulates a QML component definition."""
                          
                              def get_status(self) -> StatusStr:
                                  return STATUS.inverse[self.status()]
                          
                              def get_url(self) -> core.Url:
                                  return core.Url(self.url())
                          
                              def load_url(
                                  self,
                                  url: datatypes.UrlType,
                                  mode: CompilationModeStr
                                  | QtQml.QQmlComponent.CompilationMode = "prefer_synchronous",
                              ):
                                  qurl = (
                                      core.Url.from_user_input(url)
                                      if isinstance(url, str)
                                      else datatypes.to_url(url)
                                  )
                                  self.loadUrl(qurl, COMPILATION_MODES.get_enum_value(mode))
                          

                          QmlEngine

                          Bases: QmlEngineMixin, QQmlEngine

                          Environment for instantiating QML components.

                          Source code in prettyqt/qml/qmlengine.py
                          class QmlEngine(QmlEngineMixin, qml.QQmlEngine):
                              """Environment for instantiating QML components."""
                          

                          QmlEngineMixin

                          Bases: JSEngineMixin, _QmlEngineBase

                          Source code in prettyqt/qml/qmlengine.py
                          class QmlEngineMixin(qml.JSEngineMixin, _QmlEngineBase):
                              def set_object_ownership(
                                  self,
                                  obj: core.QObject,
                                  mode: ObjectOwnershipStr | qml.QQmlEngine.ObjectOwnership,
                              ):
                                  """Set the object ownership.
                          
                                  Args:
                                      obj: object to set ownership for
                                      mode: object ownership to use
                                  """
                                  self.setObjectOwnership(obj, OBJECT_OWNERSHIP.get_enum_value(mode))
                          
                              def get_object_ownership(self, obj: core.QObject) -> ObjectOwnershipStr:
                                  """Return object ownership.
                          
                                  Returns:
                                      object ownership
                                  """
                                  return OBJECT_OWNERSHIP.inverse[self.objectOwnership(obj)]
                          
                              def add_import_path(self, path: datatypes.PathType):
                                  self.addImportPath(os.fspath(path))
                          
                              def add_plugin_path(self, path: datatypes.PathType):
                                  self.addPluginPath(os.fspath(path))
                          
                              def get_plugin_paths(self) -> list[pathlib.Path]:
                                  return [pathlib.Path(p) for p in self.pluginPathList()]
                          
                              def get_import_paths(self) -> list[pathlib.Path]:
                                  return [pathlib.Path(p) for p in self.importPathList()]
                          
                              def set_base_url(self, url: str | core.QUrl):
                                  if isinstance(url, str):
                                      url = core.QUrl(url)
                                  self.setBaseUrl(url)
                          
                              def get_base_url(self) -> core.Url:
                                  return core.Url(self.baseUrl())
                          
                              def set_offline_storage_path(self, path: datatypes.PathType):
                                  self.setOfflineStoragePath(os.fspath(path))
                          

                          get_object_ownership(obj: core.QObject) -> ObjectOwnershipStr

                          Return object ownership.

                          Source code in prettyqt/qml/qmlengine.py
                          def get_object_ownership(self, obj: core.QObject) -> ObjectOwnershipStr:
                              """Return object ownership.
                          
                              Returns:
                                  object ownership
                              """
                              return OBJECT_OWNERSHIP.inverse[self.objectOwnership(obj)]
                          

                          set_object_ownership(obj: core.QObject, mode: ObjectOwnershipStr | qml.QQmlEngine.ObjectOwnership)

                          Set the object ownership.

                          Parameters:

                          Name Type Description Default
                          obj QObject

                          object to set ownership for

                          required
                          mode ObjectOwnershipStr | ObjectOwnership

                          object ownership to use

                          required
                          Source code in prettyqt/qml/qmlengine.py
                          def set_object_ownership(
                              self,
                              obj: core.QObject,
                              mode: ObjectOwnershipStr | qml.QQmlEngine.ObjectOwnership,
                          ):
                              """Set the object ownership.
                          
                              Args:
                                  obj: object to set ownership for
                                  mode: object ownership to use
                              """
                              self.setObjectOwnership(obj, OBJECT_OWNERSHIP.get_enum_value(mode))
                          

                          QmlError

                          Bases: QQmlError

                          Encapsulates a QML error.

                          Source code in prettyqt/qml/qmlerror.py
                          class QmlError(QtQml.QQmlError):
                              """Encapsulates a QML error."""
                          
                              def __repr__(self):
                                  return get_repr(self, self.toString())
                          
                              def get_url(self) -> core.Url:
                                  return core.Url(self.url())
                          
                              def __str__(self):
                                  return self.toString()
                          

                          QmlExpression

                          Bases: ObjectMixin, QQmlExpression

                          Evaluates JavaScript in a QML context.

                          Source code in prettyqt/qml/qmlexpression.py
                          class QmlExpression(core.ObjectMixin, qml.QQmlExpression):
                              """Evaluates JavaScript in a QML context."""
                          
                              def get_source_file(self) -> pathlib.Path | None:
                                  return pathlib.Path(source) if (source := self.sourceFile()) else None
                          
                              def get_error(self) -> qml.QmlError:
                                  return qml.QmlError(self.error())
                          

                          QmlImageProviderBase

                          Bases: QmlImageProviderBaseMixin, QQmlImageProviderBase

                          Used to register image providers in the QML engine.

                          Source code in prettyqt/qml/qmlimageproviderbase.py
                          class QmlImageProviderBase(QmlImageProviderBaseMixin, QtQml.QQmlImageProviderBase):
                              """Used to register image providers in the QML engine."""
                          

                          QmlParserStatus

                          Bases: QmlParserStatusMixin, QQmlParserStatus

                          Updates on the QML parser state.

                          Source code in prettyqt/qml/qmlparserstatus.py
                          class QmlParserStatus(QmlParserStatusMixin, QtQml.QQmlParserStatus):
                              """Updates on the QML parser state."""
                          

                          QmlProperty

                          Bases: QQmlProperty

                          Abstracts accessing properties on objects created from QML.

                          Source code in prettyqt/qml/qmlproperty.py
                          class QmlProperty(QtQml.QQmlProperty):
                              """Abstracts accessing properties on objects created from QML."""
                          
                              def __repr__(self):
                                  return get_repr(self, self.object())
                          
                              def get_method(self) -> core.MetaMethod:
                                  return core.MetaMethod(self.method())
                          
                              def get_property(self) -> core.MetaProperty:
                                  return core.MetaProperty(self.property())
                          
                              def get_property_type_category(self) -> PropertyTypeCategoryStr:
                                  return PROPERTY_TYPE_CATEGORY.inverse[self.propertyTypeCategory()]
                          
                              def get_type(self) -> TypeStr:
                                  return TYPE.inverse[self.type()]
                          

                          QmlPropertyMap

                          Bases: QmlPropertyMapMixin, QQmlPropertyMap, MutableMapping

                          Allows you to set key-value pairs that can be used in QML bindings.

                          Source code in prettyqt/qml/qmlpropertymap.py
                          class QmlPropertyMap(
                              QmlPropertyMapMixin,
                              QtQml.QQmlPropertyMap,
                              MutableMapping,
                              metaclass=datatypes.QABCMeta,
                          ):
                              """Allows you to set key-value pairs that can be used in QML bindings."""
                          
                              def __repr__(self):
                                  return get_repr(self, dict(self))
                          
                              def __setitem__(self, key: str, value: datatypes.Variant):
                                  self.insert(key, value)
                          
                              def __getitem__(self, key: str) -> datatypes.Variant:
                                  if not self.contains(key):
                                      raise KeyError(key)
                                  return self.value(key)
                          
                              def __delitem__(self, key: str):
                                  self.clear(key)
                          
                              def __contains__(self, key):
                                  return self.contains(key)
                          
                              def __bool__(self):
                                  return not self.isEmpty()
                          
                              def __iter__(self):
                                  return iter(self.keys())
                          
                              def __len__(self):
                                  return self.count()