PyQt中QThread多线程的正确用法

Posted flyflit

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PyQt中QThread多线程的正确用法相关的知识,希望对你有一定的参考价值。

先贴几篇有意思的讨论

https://www.qt.io/blog/2010/06/17/youre-doing-it-wrong#commento-login-box-container

https://www.qt.io/blog/2006/12/04/threading-without-the-headache

 

https://woboq.com/blog/qthread-you-were-not-doing-so-wrong.html

 

https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

 

https://stackoverflow.com/questions/16879971/example-of-the-right-way-to-use-qthread-in-pyqt

 

https://stackoverflow.com/questions/6783194/background-thread-with-qthread-in-pyqt

 

So, the conclusion is:
1. Don‘t read Qt 4.6 docs, it is wrong as it says "To create your own threads, subclass QThread and reimplement run()." http://doc.qt.nokia.com/4.6...

  1. Don‘t read the given "best resource", it is also wrong, because it also subclasses QThread: "...just a small amount of work: subclass QThread and reimplement run().." http://labs.trolltech.com/b...

The correct answer is in the shortest blog given in the comments: http://labs.trolltech.com/b...
Here‘s a shortened snippet from it for those who don‘t want to dig into the tarball:

class Producer : public QObject
{
Q_OBJECT
public slots:
void produce() { ...emit produced(&data)...emit finished().. }
signals:
void produced(QByteArray *data);
void finished();
};

class Consumer : public QObject
{
Q_OBJECT
public slots:
void consume(QByteArray *data) { ...emit consumed()...emit finished().. }
signals:
void consumed();
void finished();
};

int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
// create the producer and consumer and plug them together
Producer producer;
Consumer consumer;
producer.connect(&consumer, SIGNAL(consumed()), SLOT(produce()));
consumer.connect(&producer, SIGNAL(produced(QByteArray *)), SLOT(consume(QByteArray *)));

// they both get their own thread  
QThread producerThread;  
producer.moveToThread(&producerThread);  
QThread consumerThread;  
consumer.moveToThread(&consumerThread);

// start producing once the producer‘s thread has started  
producer.connect(&producerThread, SIGNAL(started()), SLOT(produce()));

// when the consumer is done, it stops its thread  
consumerThread.connect(&consumer, SIGNAL(finished()), SLOT(quit()));  
// when consumerThread is done, it stops the producerThread  
producerThread.connect(&consumerThread, SIGNAL(finished()), SLOT(quit()));  
// when producerThread is done, it quits the application  
app.connect(&producerThread, SIGNAL(finished()), SLOT(quit()));

// go!  
producerThread.start();  
consumerThread.start();  
return app.exec();  

}

以上是关于PyQt中QThread多线程的正确用法的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5 UI 制作一个豆瓣电影信息查看器,初识QThread多线程...

PyQt5中多线程模块QThread解决界面卡顿无响应问题,线程池ThreadPoolExecutor解决多任务耗时操作问题

PyQt5中多线程模块QThread解决界面卡顿无响应问题,线程池ThreadPoolExecutor解决多任务耗时操作问题

PyQt5中多线程模块QThread解决界面卡顿无响应问题,线程池ThreadPoolExecutor解决多任务耗时操作问题

Python Qt GUI设计:多线程中信号与槽的使用(基础篇—9)

在 PyQt4 中使用 QThread 运行线程时更新变量值