| 16 | #include <QTimer> |
| 17 | |
| 18 | class MyObject : public QObject |
| 19 | { |
| 20 | Q_OBJECT |
| 21 | public: |
| 22 | explicit MyObject(QObject *parent = nullptr) |
| 23 | : QObject(parent) |
| 24 | , c(new QObject(this)) |
| 25 | , p1(new QObject(this)) |
| 26 | , p2(new QObject(this)) |
| 27 | { |
| 28 | c->setObjectName(QStringLiteral("MovingSubtree")); |
| 29 | |
| 30 | auto t = new QTimer(this); |
| 31 | t->start(10000); |
| 32 | connect(t, &QTimer::timeout, this, &MyObject::reparent); |
| 33 | |
| 34 | auto gc = new QObject(c); |
| 35 | new QObject(gc); |
| 36 | c->setParent(p1); |
| 37 | } |
| 38 | |
| 39 | public slots: |
| 40 | void reparent() |
| 41 | { |
| 42 | if (c->parent() == p1) |
| 43 | c->setParent(p2); |
| 44 | else if (c->parent() == p2) |
| 45 | c->setParent(nullptr); |
| 46 | else |
| 47 | c->setParent(p1); |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | QObject *c, *p1, *p2; |