开发者

QT Thread_ Issues

I am using the following code.. But I got the error like

THREAD Started  
QObject: Cannot create children for a parent that is in a different thread.
(Parent is CGNetwork(0x10151d0d0), parent's thread is QThre开发者_运维问答ad(0x1016015b0), current thread is RenderThread(0x10155de40)

Code:

RenderThread.cpp 
RenderThread::RenderThread(CGNetwork *cgnetwork)
{
    cityUrl = "http://112.138.3.181/City/Cities";
    categoryUrl = "http://112.138.3.181/City/Categories";
    cgnetworks = cgnetwork;
    start();
}
void RenderThread::run()
{
    qDebug()<< "THREAD Started";
    cgnetworks->getCityList(cityUrl);
}

Please help me. Thanks in advance.


Every QObject belongs to a QThread. You're attempting to create a QObject in a different thread than the one it was created with.

Use QObject::moveToThread to move cgnetwork to your RenderThread.


From Qt's source code (circa 2006), the constructor for QObject(QObject*) contains the following:

if (parent && parent->d_func()->threadData != d->threadData) {
        qWarning("QObject: Cannot create children for a parent that is in a different thread.");

You can see that whatever d_func is, it contains a pointer to something called threadData. What this statement means is that if the parent of this object exists, and the parent's threadData is not == (probably comparing by pointer) to this object's (the child's) thread data, then you get this warning.

So, based off the Qt source code, it looks like you're attempting to create an object in the new thread object and make its parent some other object than the thread object.

The simple solution: don't do this. find another way.

The more advanced solution: create a slot in the parent and connect it to a signal in the child. The slot actually creates the child, the signal is fired when you want it created.

http://www.koders.com/cpp/fid824DA2E851F8AF2534234010E4E72BA361F9648A.aspx?s=mdef%3Asocket

By the way, this is not the first time I've found something in Qt by browsing source code. Often times you can find the source for the class you are interested in and see what's up by looking there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜