简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM