qttest.cpp 1.4 KB

123456789101112131415161718192021222324252627282930
  1. // Copyright (C) 2021 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. /*********************************************************************
  4. * INJECT CODE
  5. ********************************************************************/
  6. // @snippet qsignalspy-signal
  7. auto *signalInst = reinterpret_cast<PySideSignalInstance *>(%PYARG_1);
  8. PyObject *emitterPyObject = PySide::Signal::getObject(signalInst);
  9. QObject* emitter = %CONVERTTOCPP[QObject *](emitterPyObject);
  10. QByteArray signature = PySide::Signal::getSignature(signalInst);
  11. if (!signature.isEmpty())
  12. signature.prepend('2'); // SIGNAL() macro
  13. if (emitter == nullptr || signature.isEmpty()) {
  14. QByteArray error = QByteArrayLiteral("Wrong parameter (")
  15. + PepType_GetFullyQualifiedNameStr(Py_TYPE(%PYARG_1))
  16. + QByteArrayLiteral(") passed, QSignalSpy requires a signal.");
  17. PyErr_SetString(PyExc_ValueError, error.constData());
  18. return -1;
  19. }
  20. // PySide::Signal::getObject() increments the refcount for emitterPyObject,
  21. // but there is nothing that decrements the count when the spy goes out of
  22. // scope. It doesn't seem like QSignalSpy should prevent the target object
  23. // from being garbage collected. So we need to decrement the refcount here.
  24. Py_DECREF(emitterPyObject);
  25. %0 = new QSignalSpy(emitter, signature.constData());
  26. // @snippet qsignalspy-signal