Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use Qt threading without inheriting any Qt object?

The only way to enable threading demonstrated in qt documentation is through inheriting QThread and then override its run() method.

class MyThread : public QThread
 {
 public:
     void run();
 };

 void MyThread::run()
 {
     QTcpSocket socket;
     // connect QTcpSocket's signals somewhere meaningful
     ...
     socket.connectToHost(hostName, portNumber);
     exec();
 }

I wonder if there is any way to use qt thread without ever inheriting from any qt objects?

like image 642
lyxera Avatar asked Sep 17 '09 08:09

lyxera


1 Answers

You can use multithreading without inheriting from QObject with QtConcurrent::run():

QFuture QtConcurrent::run ( Function function, ... )
Runs function in a separate thread. The thread is taken from the global QThreadPool. Note that the function may not run immediately; the function will only be run when a thread is available.

like image 134
rpg Avatar answered Oct 15 '22 07:10

rpg