moveToThread vs deriving from QThread in Qt

前端 未结 4 1017
南笙
南笙 2021-02-01 17:07

When should moveToThread be preferred over subclassing QThread?

This link shows that both methods work. On what basis should I decide what to u

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-01 17:36

    As a starting point: use neither. In most cases, you have a unit of work that you wish to run asynchronously. Use QtConcurrent::run for that.

    If you have an object that reacts to events and/or uses timers, it's a QObject that should be non-blocking and go in a thread, perhaps shared with other objects.

    Such an object can also wrap blocking APIs.

    Subclassing QThread is never necessary in practice. It's like subclassing QFile. QThread is a thread handle. It wraps a system resource. Overloading it is a bit silly.

提交回复
热议问题