7

编辑:

我试着做你们在评论中告诉我的事情......:

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 天,无法得到解决方案。

标题:

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

当 manager->get() 被执行时,我收到以下错误:

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() 被执行时:

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

我以以下方式启动此线程:

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

为什么会这样?如何解决这个问题?

4

6 回答 6

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

你得到这个是因为你在 Citizen 的构造函数中创建了 QNetworkAccessmanager,它在“原始”线程中被调用。当 Citizen 对象移动到新线程时,QNetworkAccessmanager 仍然将其线程关联设置为原始线程,但在运行调用中它将尝试在新线程中创建 QNetworkReply 对象(可能还有其他对象)。这会产生上面的警告。

如果您在运行槽中创建管理器(或在 Citizen 对象移动到新线程后的任何时间),则不会发生这种情况。

但是,您仍然有一些问题。例如,Citizen 类实际上不需要是 QThread。它不必要地使它复杂化。将 QObject 子类化就足以满足您的目的(afaict)。只需制作一个普通插槽并将其连接到 QThread::started() 信号。正如 OrcunC 指出的那样,您需要确保 QThread 实例的范围正确。

有关线程的更多信息:http: //blog.qt.io/blog/2010/06/17/youre-doing-it-wrong/

例子:

QThread *thread = new QThread;
thread->start();
Citizen *worker = new Citizen;
worker->moveToThread(thread);

//startWorking can be equivalent of the run function
//in your current implementation and this is where you should
//create the QNetworkAccessManager
QMetaObject::invokeMethod(worker,"startWorking");
于 2011-08-02T10:21:57.867 回答
3

我将尝试回答您为什么看到QThread: Destroyed while thread is still running错误。

如果你这样做

void mtMethod () {

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

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

退出函数时线程对象将被销毁,但已启动的线程仍在运行!Qt 警告您应该停止线程或在更大范围内创建线程对象。(即使其成为您班级的成员函数)。像这样的东西:

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();
}
于 2011-08-02T06:14:13.893 回答
1

在调用 run 之前,我不相信新线程存在。所以构造函数是一个不同于run()的线程。如果将管理器对象的创建从构造函数移到 run() 会发生什么?我想这将解决第一个错误,如果不是计时器错误也是如此。

另外,我认为很多人仍然像你一样构建线程,但你可能想看看这个

于 2011-08-01T16:02:43.913 回答
1

您需要考虑线程亲和力。该错误消息不是说谎或疯狂,它确切地告诉您出了什么问题。

于 2011-08-01T16:05:18.463 回答
1

您的问题主要是由于尝试将 QThread 子类化。即使文档推荐它,它也不是使用 QThread 的最佳方式。请参阅此问题和答案以获取更多信息和链接。

于 2011-08-01T16:06:11.817 回答
1

我还没有弄清楚 startTimers 错误,尽管它可能与第一个错误有关。无论如何,您应该能够修复第一个错误。我在 Qt 中遇到过这个问题几次,我发现这是解决它的“最佳”方法是创建一个初始化函数和一个清理函数。类的所有成员都是在调用 run 之前初始化为 NULL 的指针。请注意,“最好”是用引号引起来的,因为肯定会有不同的意见,但它适用于我的大多数情况。

标题

class Citizen : public QThread {
   Q_OBJECT

   QNetworkAccessManager * manager;

   private slots:
      void onReplyFinished(QNetworkReply* net_reply);
   public:
      Citizen(QObject * parent);
      void run();

   private:
      void initialize();
      void cleanUp();
 };

执行

Citizen::Citizen(QObject * parent) :
   manager(NULL) {
}

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

void Citizen::run() {
   initialize();
   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();

   cleanUp();
}

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

void Citizen::cleanUp() {
   delete manager;
   disconnect(_net_acc_mgr, SIGNAL(finished(QNetworkReply*)),
              this, SLOT(onReplyFinished(QNetworkReply*)));
}
于 2011-08-01T22:43:03.117 回答