Skip to content

JSValue

Base classes

Name Children Inherits
QJSValue
PySide6.QtQml
QJSValue(self, other: PySide6.QtQml.QJSValue, /) -> None

⋔ Inheritance diagram

graph TD
  1437519632["qml.JSValue"]
  2927405082["QtQml.QJSValue"]
  37984305["Shiboken.Object"]
  2751680898["builtins.object"]
  2927405082 --> 1437519632
  37984305 --> 2927405082
  2751680898 --> 37984305

🛈 DocStrings

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()

:fa-brands-github: Show source on GitHub