PyQt中QThread多线程的正确用法
2021-01-15 22:11
标签:blog for void wrong mount nec producer answer details 先贴几篇有意思的讨论 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: The correct answer is in the shortest blog given in the comments: http://labs.trolltech.com/b... class Producer : public QObject class Consumer : public QObject int main(int argc, char **argv) } PyQt中QThread多线程的正确用法 标签:blog for void wrong mount nec producer answer details 原文地址:https://www.cnblogs.com/flyflit/p/12935157.html
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...
Here‘s a shortened snippet from it for those who don‘t want to dig into the tarball:
{
Q_OBJECT
public slots:
void produce() { ...emit produced(&data)...emit finished().. }
signals:
void produced(QByteArray *data);
void finished();
};
{
Q_OBJECT
public slots:
void consume(QByteArray *data) { ...emit consumed()...emit finished().. }
signals:
void consumed();
void finished();
};
{
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();
上一篇:Windows函数转发器
下一篇:Python之网络编程二
文章标题:PyQt中QThread多线程的正确用法
文章链接:http://soscw.com/index.php/essay/42421.html