QThread, threaded, rly?

后端 未结 1 518
轻奢々
轻奢々 2021-01-15 02:24

for some time I am fiddly now to get a massive time/cputime drain action running behind a nicly responding UI. Unfortunatly I can\'t seem to get it running and \"I think\" t

相关标签:
1条回答
  • 2021-01-15 02:42

    Proper usage of QThread is a common problem among Qt users. This blog post at Qt Labs explains it nicely.

    In short, you should not subclass QThread to contain code that you want to run in that thread. You should wrap your code up in a QObject subclass, instantiate it and use a QObject::moveToThread to move your object into the QThread context so that processing will occur in the context of that QThread. You can then have slots in your QObject subclass that can safely be connected to another thread but will run in the context you expect.

    Some may argue that subclassing QThread should be fine if you can easily fit what you want to do inside the run() method and don't need much (or any) external interaction but even in this simple case I would favor a separate class for better encapsulation.

    0 讨论(0)
提交回复
热议问题