QObject的:不能对父母是在不同的线程创建的孩子 [英] QObject: Cannot create children for a parent that is in a different thread

查看:5944
本文介绍了QObject的:不能对父母是在不同的线程创建的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:

我试图做什么你们告诉我的意见......

I tried doing what you guys told me in comments ... :

        Citizen * c = new Citizen(this);

        QThread thread;
        c->moveToThread(&thread);

        connect(&thread, SIGNAL(started()), c, SLOT(ProcessActions()));
        thread.start();

这会产生更多的错误:

QThread: Destroyed while thread is still running
ASSERT failure in QThread::setTerminationEnabled(): "Current thread was not started with QThread.", file c:\ndk_buildrepos\qt-desktop\src\corelib\thread\qthread_win.cpp, line 542
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
QObject::killTimers: timers cannot be stopped from another thread

我有这个错误的问题...我卡在这2天已经并不能得到解决。

I am having problems with this error ... I'm stuck on this for 2 days already and can't get a solution.

标题:

class Citizen : public QThread
{
Q_OBJECT    
    QNetworkAccessManager * manager;

private slots:
    void onReplyFinished(QNetworkReply* net_reply);

public:
    Citizen(QObject * parent);

    void run();
};

实施

Citizen::Citizen(QObject * parent)
{
    manager = new QNetworkAccessManager;
    connect(_net_acc_mgr, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(onReplyFinished(QNetworkReply*)));
}

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

void Citizen::run()
{
    manager->get(QNetworkRequest(QUrl("http://google.com"));

    QEventLoop eLoop;
    connect(manager, SIGNAL( finished( QNetworkReply * ) ), &eLoop, SLOT(quit()));
    eLoop.exec(QEventLoop::ExcludeUserInputEvents);

    qDebug() << "loaded google!";

    exec();
}

在管理器 - > get()方法被执行,我收到以下错误:

When manager->get() gets executed, I get the following error:

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNetworkAccessManager(0xc996cf8), parent's thread is QThread(0xaba48d8), current thread is Citizen(0xca7ae08)

在eLoop.exec()被执行:

When eLoop.exec() gets executed:

QObject::startTimer: timers cannot be started from another thread

我启动该线程以下列方式:

I start this thread in the following manner:

Citizen * c = new Citizen(this);
c->start();

为什么会出现这种情况?如何解决此问题?

Why does this happen? How to solve this?

推荐答案

我只是尝试回答为什么你看到的的QThread:被毁坏,而线程仍在运行错误。

I will just try to answer why you are seeing QThread: Destroyed while thread is still running error.

如果你做到这一点。

void mtMethod () {

 Citizen * c = new Citizen(this);
 QThread thread;
 c->moveToThread(&thread);

 connect(&thread, SIGNAL(started()), c, SLOT(ProcessActions()));
 thread.start();
}

当您退出函数的线程对象将被毁灭,但已启动线程仍在运行! Qt是警告你,你要么停止线程或在更大的范围内创建线程对象。 (即让你的类的成员函数)。事情是这样的:

The thread object will be destroyed when you exit the function but the thread that has been started is still running !. Qt is warning you that you should either stop the thread or create the thread object in a bigger scope. (i.e make it a member function of your class). Something like this :

class myClass
{
virtual ~myClass ();
 QThread mythread;
};

myClass::~myClass
{
  mythread.stop ();
}

void myClass::mtMethod () {

     Citizen * c = new Citizen(this);
     c->moveToThread(&mythread);

     connect(&mythread, SIGNAL(started()), c, SLOT(ProcessActions()));
     mythread.start();
}

这篇关于QObject的:不能对父母是在不同的线程创建的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆