Skip to content

Commit d6bd0d2

Browse files
mrbean-bremenjcfr
authored andcommitted
[Backport] Add facility to set task done callback
- on async signal handlers (originally provided by @usiems) (cherry picked from commit MeVisLab/pythonqt@575c51f)
1 parent f0ffa66 commit d6bd0d2

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

src/PythonQt.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)
274274
}
275275

276276
_self->priv()->pythonQtModule().addObject("Debug", _self->priv()->_debugAPI);
277+
_self->priv()->pythonQtModule().addObject("Config", _self->priv()->_configAPI);
277278

278279
Py_INCREF((PyObject*)&PythonQtSlotDecorator_Type);
279280
Py_INCREF((PyObject*)&PythonQtSignalFunction_Type);
@@ -400,6 +401,11 @@ PythonQtPrivate::~PythonQtPrivate() {
400401
PythonQtArgumentFrame::cleanupFreeList();
401402
}
402403

404+
void PythonQtPrivate::setTaskDoneCallback(const PythonQtObjectPtr & callable)
405+
{
406+
_pyTaskDoneCallback = callable;
407+
}
408+
403409
void PythonQt::setRedirectStdInCallback(PythonQtInputChangedCB* callback, void * callbackData)
404410
{
405411
if (!callback) {
@@ -1440,6 +1446,7 @@ PythonQtPrivate::PythonQtPrivate()
14401446
_hadError = false;
14411447
_systemExitExceptionHandlerEnabled = false;
14421448
_debugAPI = new PythonQtDebugAPI(this);
1449+
_configAPI = new PythonQtConfigAPI(this);
14431450
}
14441451

14451452
void PythonQtPrivate::setupSharedLibrarySuffixes()

src/PythonQt.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@ class PYTHONQT_EXPORT PythonQt : public QObject {
663663
};
664664

665665
class PythonQtDebugAPI;
666+
class PythonQtConfigAPI;
666667

667668
//! internal PythonQt details
668669
class PYTHONQT_EXPORT PythonQtPrivate : public QObject {
@@ -681,6 +682,10 @@ class PYTHONQT_EXPORT PythonQtPrivate : public QObject {
681682
AllDecorators = 0xffff
682683
};
683684

685+
//! Set a callable that is used as the argument for the add_done_callback for the Task
686+
//! created by checkAndRunCoroutine
687+
void setTaskDoneCallback(const PythonQtObjectPtr& callable);
688+
684689
//! get the suffixes that are used for shared libraries
685690
const QStringList& sharedLibrarySuffixes() { return _sharedLibrarySuffixes; }
686691

@@ -861,6 +866,8 @@ class PYTHONQT_EXPORT PythonQtPrivate : public QObject {
861866
PythonQtObjectPtr _pySourceFileLoader;
862867
PythonQtObjectPtr _pySourcelessFileLoader;
863868

869+
PythonQtObjectPtr _pyTaskDoneCallback;
870+
864871
//! the cpp object wrapper factories
865872
QList<PythonQtCppWrapperFactory*> _cppWrapperFactories;
866873

@@ -873,6 +880,7 @@ class PYTHONQT_EXPORT PythonQtPrivate : public QObject {
873880
PythonQt::ProfilingCB* _profilingCB;
874881

875882
PythonQtDebugAPI* _debugAPI;
883+
PythonQtConfigAPI* _configAPI;
876884

877885
int _initFlags;
878886
int _PythonQtObjectPtr_metaId;

src/PythonQtStdDecorators.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,17 @@ const QMetaObject* PythonQtStdDecorators::metaObject( QObject* obj )
354354
return obj->metaObject();
355355
}
356356

357+
//---------------------------------------------------------------------------
358+
359+
void PythonQtConfigAPI::setTaskDoneCallback(PyObject* object)
360+
{
361+
if (PythonQt::self()) {
362+
PythonQt::self()->priv()->setTaskDoneCallback(object);
363+
}
364+
}
365+
366+
//---------------------------------------------------------------------------
367+
357368
bool PythonQtDebugAPI::isOwnedByPython( PyObject* object )
358369
{
359370
if (PyObject_TypeCheck(object, &PythonQtInstanceWrapper_Type)) {

src/PythonQtStdDecorators.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ public Q_SLOTS:
173173

174174
};
175175

176+
//! Some methods to set properties of PythonQt from Python
177+
class PYTHONQT_EXPORT PythonQtConfigAPI : public QObject
178+
{
179+
Q_OBJECT
180+
public:
181+
PythonQtConfigAPI(QObject* parent):QObject(parent) {};
182+
183+
public slots:
184+
//! Set a callable that is used as the argument for the add_done_callback for the Task/Future
185+
//! created when, e.g., an async function is connected to signal.
186+
void setTaskDoneCallback(PyObject* object);
187+
};
188+
189+
176190
//! Some helper methods that allow testing of the ownership
177191
class PYTHONQT_EXPORT PythonQtDebugAPI : public QObject
178192
{

0 commit comments

Comments
 (0)