class MyThread : public QThread { public: void run() override { // thread code here } }; MyThread thread; thread.start();
class Worker : public QObject { Q_OBJECT public slots: void doWork() { // thread code here } }; Worker worker; QThread thread; worker.moveToThread(&thread); QObject::connect(&thread, &QThread::started, &worker, &Worker::doWork); thread.start();In this example, we create a separate worker object that will run in the thread. We move the object to the thread using the moveToThread function. Then we connect the started signal of the QThread object to the doWork slot of the worker, and start the thread. The package library for QThread is the Qt Core library, which is part of the Qt framework.