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

The only way to enable threads, as demonstrated in the qt documentation, is to inherit QThread and then override its run () method.

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

I wonder if there is a way to use a qt stream without inheriting from any qt objects?

+6
c ++ multithreading qt
source share
3 answers

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

QFuture QtConcurrent :: run (function function, ...)
Performs a function in a separate thread. The thread is taken from the global QThreadPool. Please note that the function may not work immediately; the function will be launched only when the stream is available.

+10
source share

QThread itself is derived from QObject. You need to override its startup method to use it, so you need to inherit from QObject to use QThread.

Why don't you want to inherit from QObject?

+3
source share

If you don't want to inherit QThread, you can create a wrapper that inherits QThread and takes your objects as an argument, for example. via the IRunnable interface (which you do and allow your inheritance classes to inherit).

+2
source share

All Articles