Henricus V.
Henricus V.

Reputation: 948

In Qt, what happens to an object when a thread finishes?

I have a simple QObject:

class Engine : public QObject
{
    Q_OBJECT
public:
    explicit Engine(QObject* parent = 0);
signals:
    void finished();
public slots:
    void start();
};

An instance Engine* engine is stored inside the main window class. When a button is pressed, the following happens:

QThread* thread = new QThread;
engine->moveToThread(thread);
connect(engine, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
connect(thread, SIGNAL(started()), engine, SLOT(start()));
connect(engine, SIGNAL(finished()), thread, SLOT(quit()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();

My question is, what happens to engine after thread finishes? Can I create another thread and move engine to that thread, and repeat everything again?

Upvotes: 3

Views: 521

Answers (1)

perencia
perencia

Reputation: 1552

What happens to engine after thread finishes?

What happens with the object is independent of it being moved to a thread. When you "move" you are not doing a real move, you are just telling to execute some of the code on a thread. The object will get destroyed as usual (out of scope or delete for heap allocated).

Can I create another thread and move engine to that thread, and repeat everything again?

Yes, as long as the object still exists.

Upvotes: 3

Related Questions