我試着做什麼你們告訴我在評論...:
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();
}
當管理器 - > 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()獲取e xecuted:
QObject::startTimer: timers cannot be started from another thread
我開始這個線程以下列方式:
Citizen * c = new Citizen(this);
c->start();
爲什麼會出現這種情況?如何解決這個問題?
有這個主題[線程,事件和的QObject]良好的製品(http://qt-project.org/wiki/ Threads_Events_QObjects#913fb94dd61f1a62fc809f8d842c3afa)。如果你的二等公民工作在一個線程,你不應該的QThread繼承,因爲繼承從QThread的目的不是做在線程一些工作,但管理線程。 –