【发布时间】:2018-05-31 21:08:28
【问题描述】:
我在另一个线程中创建了一个 QNetworkAccessManager。
该网络只能在 MyMegaThread 中使用。
QNetworkAccessManager 是从线程的run 方法创建的:
mp_manager.reset(new QNetworkAccessManager{this});
在创建时,我在控制台中收到这样的消息:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is MyMegaThread(0x237eabd0ee0), parent's thread is QThread(0x237e70742a0), current thread is MyMegaThread(0x237eabd0ee0)
此消息完全无害,但我想知道经理应该有哪个父母。
我怀疑这是因为 MyMegaThread 实例是在主线程中创建的,但我需要在 MyMegaThread 中创建父级。
这样做的惯用方式是什么?
【问题讨论】:
-
可能你不应该使用父级,QNetworkAccessManager from different thread
-
是的,好主意。我可以将 QNetworkAccessManager 放入智能指针以避免复杂性。
标签: c++ multithreading qt networking