def connect_log_slot(obj: QObject) -> None: """Helper function to connect all signals to a logging slot.""" metaobj = obj.metaObject() for i in range(metaobj.methodCount()): meta_method = metaobj.method(i) qtutils.ensure_valid(meta_method) if meta_method.methodType() == QMetaMethod.Signal: name = meta_method.name().data().decode('ascii') if name != 'destroyed': signal = getattr(obj, name) try: signal.connect(functools.partial( log_slot, obj, signal)) except TypeError: # pragma: no cover pass
def getNotifySignal(obj: QtCore.QObject, prop: str) -> QtCore.pyqtBoundSignal: """Get the notify signal of an object's property Parameters ---------- obj Object instance prop Property name Returns ------- Bound notify signal """ mo = obj.metaObject() idx = mo.indexOfProperty(prop) if idx < 0: raise ValueError(f"{obj} has no property `{prop}`") sig = mo.property(idx).notifySignal() if not sig.isValid(): raise ValueError(f"{obj}'s property `{prop}` has no notify signal") return getattr(obj, bytes(sig.name()).decode())
def check_has_signal(obj: QObject, signal: Union[pyqtSignal, str]): """ Check that given object has given signal. :param obj: object on which to check for existence of Qt signal :param signal: a signal (return value from pyqtSignal(...)) or string of a signal name :raises: RuntimeError if it does not have the signal :raises: Attribute error if signal is a string and there is no such attribute on obj """ if isinstance(signal, str): signal = getattr(obj, signal) meta = obj.metaObject() for i in range(0, meta.methodCount()): meta_meth = meta.method(i) if meta_meth.methodType() == QMetaMethod.Signal: meth_name = bytes(meta_meth.name()).decode() sig_meta = getattr(obj, meth_name) if sig_meta.signal == signal.signal: return raise RuntimeError("Object of type {} does not have a signal {}")
def toXml( cls, obj_: QtCore.QObject, include_children: bool = True, include_complex_types: bool = False, ) -> QtXml.QDomDocument: """ Convert an object to xml. @param obj_. Object to be processed @param include_children. Include children of the given object @param include_complex_types. Include complex children @return xml of the given object """ xml_ = QtXml.QDomDocument() if not obj_: return xml_ e = xml_.createElement(type(obj_).__name__) e.setAttribute("class", type(obj_).__name__) xml_.appendChild(e) _meta = obj_.metaObject() i = 0 # _p_properties = [] for i in range(_meta.propertyCount()): mp = _meta.property(i) # if mp.name() in _p_properties: # i += 1 # continue # _p_properties.append(mp.name()) val = getattr(obj_, mp.name(), None) try: val = val() except Exception: pass if val is None: i += 1 continue val = str(val) if not val and not include_complex_types: i += 1 continue e.setAttribute(mp.name(), val) i += 1 if include_children: for child in obj_.children(): itd = cls.toXml(child, include_children, include_complex_types) xml_.firstChild().appendChild(itd.firstChild()) return xml_