EDIT:QObject: Không thể tạo trẻ em cho cha mẹ đó là trong một thread khác
tôi đã cố gắng làm những gì cậu nói với tôi trong ý kiến ...:
Citizen * c = new Citizen(this);
QThread thread;
c->moveToThread(&thread);
connect(&thread, SIGNAL(started()), c, SLOT(ProcessActions()));
thread.start();
Điều này tạo ra nhiều hơn lỗi:
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
Tôi đang gặp sự cố với lỗi này ... Tôi bị kẹt trong 2 ngày này và không thể có giải pháp.
Tiêu đề:
class Citizen : public QThread
{
Q_OBJECT
QNetworkAccessManager * manager;
private slots:
void onReplyFinished(QNetworkReply* net_reply);
public:
Citizen(QObject * parent);
void run();
};
Thực hiện:
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();
}
Khi Manager-> get() được thực thi, tôi nhận được lỗi sau:
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)
Khi eLoop.exec() được e xecuted:
QObject::startTimer: timers cannot be started from another thread
tôi bắt đầu chủ đề này theo cách sau đây:
Citizen * c = new Citizen(this);
c->start();
Tại sao điều này xảy ra? Làm thế nào để giải quyết điều này?
Có một bài viết tốt cho chủ đề này [Chủ đề, sự kiện và QObjects] (http://qt-project.org/wiki/ Threads_Events_QObjects # 913fb94dd61f1a62fc809f8d842c3afa). Nếu lớp của bạn Citizen làm việc trong một chủ đề bạn không nên kế thừa nó từ QThread, bởi vì mục đích thừa kế từ QThread không phải là để làm một số công việc trong thread, nhưng quản lý các chủ đề. –