aboutsummaryrefslogtreecommitdiff
path: root/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_holder.h
blob: 39200d512c178c7b55e04c9517b4734a1db17d8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef PYTHON_PYBIND11_QT_HOLDER_HPP
#define PYTHON_PYBIND11_QT_HOLDER_HPP

#include <QObject>

#include <pybind11/pybind11.h>

namespace pybind11::detail::qt {

    class qobject_holder_impl : public QObject {
        object p_;

    public:
        /**
         * @brief Construct a new qobject holder linked to the given QObject and
         * maintaining the given python object alive.
         *
         * @param p Parent of this holder.
         * @param o Python object to keep alive.
         */
        qobject_holder_impl(QObject* p, object o) : p_{o} { setParent(p); }

        template <class U>
        qobject_holder_impl(U* p)
            : qobject_holder_impl{p, reinterpret_borrow<object>(cast(p))}
        {
        }

        ~qobject_holder_impl()
        {
            gil_scoped_acquire s;
            p_ = object();
        }
    };

}  // namespace pybind11::detail::qt

namespace pybind11::qt {

    template <class Type>
    class qobject_holder {
        using type = Type;

        type* qobj_;

    public:
        qobject_holder(type* qobj) : qobj_{qobj}
        {
            new detail::qt::qobject_holder_impl(qobj_);
        }

        type* get() { return qobj_; }
    };

}  // namespace pybind11::qt

PYBIND11_DECLARE_HOLDER_TYPE(T, ::pybind11::qt::qobject_holder<T>)

#endif