Skip to content

JSValue

Qt Base Class: QJSValue

Signature: QJSValue(self, other: Union[PySide6.QtQml.QJSValue, PySide6.QtQml.QJSValue.SpecialValue, bool, str, bytes, float, int]) -> None QJSValue(self, str: bytes) -> None QJSValue(self, value: PySide6.QtQml.QJSValue.SpecialValue = Instance(PySide6.QtQml.QJSValue.SpecialValue.UndefinedValue)) -> None QJSValue(self, value: str) -> None QJSValue(self, value: bool) -> None QJSValue(self, value: float) -> None QJSValue(self, value: int) -> None QJSValue(self, value: int) -> None

Base classes

Name Children Inherits
QJSValue
PySide6.QtQml
QJSValue(self, other: Union[PySide6.QtQml.QJSValue, PySide6.QtQml.QJSValue.SpecialValue, bool, str, bytes, float, int]) -> None

⋔ Inheritance diagram

graph TD
  1473572325728["qml.JSValue"]
  1473572255456["QtQml.QJSValue"]
  1473291690208["Shiboken.Object"]
  140713234304496["builtins.object"]
  1473572255456 --> 1473572325728
  1473291690208 --> 1473572255456
  140713234304496 --> 1473291690208

🛈 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
        else:
            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():
                array = cls(jsengine.newArray())
                for k, v in obj.items():
                    array.setProperty(k, cls.from_object(v, jsengine))
                return array
            case _:
                try:
                    return cls(obj)
                except TypeError:
                    logger.debug(f"unknown type: {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():
            array = cls(jsengine.newArray())
            for k, v in obj.items():
                array.setProperty(k, cls.from_object(v, jsengine))
            return array
        case _:
            try:
                return cls(obj)
            except TypeError:
                logger.debug(f"unknown type: {obj}")
                return cls()