C++ QObject:无法为不同线程中的父级创建子级

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3268073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:28:54  来源:igfitidea点击:

QObject: Cannot create children for a parent that is in a different thread

c++qt

提问by Donotalo

I am using Qt 4.6.0 (32 bit) under Windows 7 Ultimate. Consider the following QThread:

我在 Windows 7 Ultimate 下使用 Qt 4.6.0(32 位)。考虑以下几点QThread

Interface

界面

class ResultThread : public QThread
{
Q_OBJECT

    QString _post_data;
    QNetworkAccessManager _net_acc_mgr;

signals:
    void onFinished(QNetworkReply* net_reply);

private slots:
    void onReplyFinished(QNetworkReply* net_reply);

public:
    ResultThread();

    void run();
    void setPostData(const QString& post_data);
};

Implementation

执行

ResultThread::ResultThread() : _net_acc_mgr(this)
{
    connect(&_net_acc_mgr, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(onReplyFinished(QNetworkReply*)));
}

void ResultThread::onReplyFinished(QNetworkReply* net_reply)
{
    emit onFinished(net_reply);
}

void ResultThread::setPostData(const QString& post_data)
{
    _post_data = post_data;
}

void ResultThread::run()
{
    _net_acc_mgr.post(QNetworkRequest(QUrl("http://[omitted]")),
                      QByteArray(_post_data.toStdString().c_str()));
}

Whenever _net_acc_mgr.post()is executed in ResultThread::run(), I got the following Application Output in Qt Creator:

每当_net_acc_mgr.post()在 中执行时ResultThread::run(),我都会在 Qt Creator 中得到以下应用程序输出:

QObject: Cannot create children for a parent that is in a different thread.

(Parent is QNetworkAccessManager(0x22fe58), parent's thread is QThread(0x9284190), current thread is ResultThread(0x22fe48)

QObject:无法为不同线程中的父级创建子级。

(父为QNetworkAccessManager(0x22fe58),父线程为QThread(0x9284190),当前线程为ResultThread(0x22fe48)

What does this mean? How to solve it?

这是什么意思?如何解决?

回答by Gunther Piez

The run()member function is executed in a different thread, rather than the thread where QNetworkRequestManagerobject was created.

运行()成员函数在不同的线程中执行,而不是其中的线程QNetworkRequestManager被创建的对象。

This kind of different-thread problems happen all the time with Qtwhen you use multiple threads. The canonical way to solve this problem is to use signalsand slots.

当您使用多线程时,这种不同线程的问题在Qt 中一直发生。解决这个问题的规范方法是使用信号

Create a slotin the object where QNetworkRequestManagerbelongs to, create a signalin ResultThreadand connect both of the somewhere, the constructor of ResultThreadwould be a good place.

在所属的对象中创建一个,在ResultThread中QNetworkRequestManager创建一个信号并将两者连接起来,ResultThread的构造函数将是一个好地方。

The code which is currently in ResultThread::run()goes to the new slot, and is replaced by a emit(yourSignal()). If necessary send a pointer to your ResultThreadas a parameter with your emit function to gain access to member functions/variables.

当前在ResultThread::run() 中的代码转到新的slot,并由 a 替换emit(yourSignal())。如有必要,将指向您的ResultThread的指针作为参数与您的发射函数一起发送,以获得对成员函数/变量的访问。