是否可以在不继承任何Qt对象的情况下使用Qt线程?

lyx*_*era 6 c++ qt multithreading

在qt文档中演示启用线程的唯一方法是继承QThread,然后覆盖其run()方法.

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

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

我想知道是否有任何方法可以使用qt线程而无需继承任何qt对象?

rpg*_*rpg 10

您可以使用多线程而不使用QtConcurrent :: run()继承QObject:

QFuture QtConcurrent :: run(函数函数,...)
在单独的线程中运行函数.该线程取自全局QThreadPool.请注意,该功能可能无法立即运行; 该函数仅在线程可用时运行.