在Qt中,关闭线程有以下几种方法:
QThread* thread = new QThread; ... thread->quit(); thread->wait();
QThread* thread = new QThread; ... thread->requestInterruption(); while (!thread->isInterruptionRequested()) { // 线程处理逻辑 }
class MyThread : public QThread { Q_OBJECT public: MyThread() : m_stopFlag(false) {} void stop() { m_stopFlag = true; } protected: void run() override { while (!m_stopFlag) { // 线程处理逻辑 } } private: bool m_stopFlag; }; MyThread thread; ... thread.stop(); thread.wait();
以上是一些常见的关闭线程的方法,在实际使用中可以根据具体情况选择合适的方法。