久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

QObject:無法為不同線程中的父級創建子級

QObject: Cannot create children for a parent that is in a different thread(QObject:無法為不同線程中的父級創建子級)
本文介紹了QObject:無法為不同線程中的父級創建子級的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 Windows 7 Ultimate 下使用 Qt 4.6.0(32 位).考慮以下 QThread:

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

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);
};

實施

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()));
}

每當 _net_acc_mgr.post()ResultThread::run() 中執行時,我在 Qt Creator 中得到以下應用程序輸出:

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

QObject:無法為不同線程中的父對象創建子對象.

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

(父為QNetworkAccessManager(0x22fe58),父線程為QThread(0x9284190),當前線程為ResultThread(0x22fe48)

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

這是什么意思?如何解決?

What does this mean? How to solve it?

推薦答案

run() 成員函數在不同的線程中執行,而不是 QNetworkRequestManager 所在的線程對象已創建.

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

當您使用多線程時,Qt 總是會發生這種不同線程的問題.解決這個問題的規范方法是使用信號.

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

QNetworkRequestManager所屬的對象中創建一個,在ResultThread中創建一個信號并連接兩者在某處,ResultThread 的構造函數將是一個好地方.

Create a slot in the object where QNetworkRequestManager belongs to, create a signal in ResultThread and connect both of the somewhere, the constructor of ResultThread would be a good place.

當前在 ResultThread::run() 中的代碼進入新的,并被替換為 emit(yourSignal()).如有必要,將指向您的 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 ResultThread as a parameter with your emit function to gain access to member functions/variables.

這篇關于QObject:無法為不同線程中的父級創建子級的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 亚洲视频在线观看 | 久久专区| 国产精品视频区 | 99色视频| 一区二区三区在线 | 欧美在线看片 | 美女久久 | 日韩另类视频 | 精品亚洲一区二区三区 | 亚洲高清av| 色综合一区二区三区 | a欧美| 精品久久香蕉国产线看观看亚洲 | 久久久久久久久久久丰满 | 国产精品视频一区二区三 | 久久久91精品国产一区二区精品 | www.日日操 | 日韩中文字幕 | 日韩高清成人 | 午夜影院在线免费观看视频 | 日本久草 | 一区视频在线播放 | 亚洲视频一区在线 | 日韩国产精品一区二区三区 | 狠狠操电影 | 国产成人jvid在线播放 | 国产精品国产精品国产专区不蜜 | 精品三区 | 亚洲视频一区 | 国产亚洲欧美日韩精品一区二区三区 | 国产午夜精品一区二区三区嫩草 | 91.色| 亚洲少妇综合网 | 亚洲成人免费在线观看 | av在线电影网站 | 欧美国产激情 | 午夜久久久 | 亚洲国产一区二区三区在线观看 | 99国产精品久久久久老师 | 亚洲成人精选 | 日本激情一区二区 |