#includeclass MyThread : public QThread { public: void run() { // Do some work in this thread } }; int main() { MyThread thread; thread.start(); return 0; }
#includeIn this example, we create a custom thread class that emits a signal containing a result after finishing its work in the run() method. We also create a QObject-based class that contains a slot for receiving the result. We then connect the signal and slot using the QObject::connect() method before starting the thread. Library: The QThread class is part of the QtCore library in the Qt framework.class MyThread : public QThread { public: void run() { // Do some work in this thread emit resultReady(42); } signals: void resultReady(int result); }; class Worker : public QObject { Q_OBJECT public slots: void onResultReady(int result) { // Do something with the result } }; int main() { MyThread thread; Worker worker; QObject::connect(&thread, &MyThread::resultReady, &worker, &Worker::onResultReady); thread.start(); return 0; }