简体   繁体   English

(PyQt5) QThread 是抽象的 class 吗?

[英](PyQt5) is QThread an abstract class?

So apparently when we are multi-threading in pyqt by using QThread, we will have to subclass QThread and it can not be directly instantiated.所以很明显,当我们在 pyqt 中使用 QThread 进行多线程时,我们将不得不继承 QThread 并且不能直接实例化它。 this implies that QThread is an abstract class.这意味着 QThread 是一个抽象的 class。

That's fine, but the thing that i don't understand is that when we are subclassing QThread, we only override the run() method, but in order to actually get this multi-threading feature to work, we have to call the start() method of the QThread subclass which we did not override at all (AFAIK if we are subclassing an abstract class, we will have to override every method that it has in our child class otherwise it can not b instantiated).这很好,但我不明白的是,当我们继承 QThread 时,我们只覆盖了 run() 方法,但是为了真正让这个多线程功能工作,我们必须调用 start( ) QThread 子类的方法,我们根本没有重写(AFAIK,如果我们是抽象 class 的子类,我们将不得不重写它在我们的子 class 中具有的每个方法,否则它无法实例化)。

So where does the start() and finish methods come from?那么 start() 和 finish 方法从何而来?

Not every method of an abstract class is itself abstract.并非抽象 class 的每个方法本身都是抽象的。 In the case of a QThread , the methods start() and finish() will have the same behavior across all subclasses, so they have concrete implementations for you to use, but there is no base implementation of run() (since that defines what the thread does) so that is abstract and requires custom implementation.对于QThread ,方法start()finish()将在所有子类中具有相同的行为,因此它们具有供您使用的具体实现,但没有run()的基本实现(因为它定义了什么线程确实如此)所以这是抽象的并且需要自定义实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM