我使用QRunnable时遇到问题。当我试图从线程发出信号时,我遇到了运行时错误。有我的代码:
curlthread.cpp
void CurlThread::run() {
qDebug()<<"Thread started"<<act();
qDebug()<<"1234";
emit error(url(),act());
qDebug()<<"5678";
return;
if (system(NULL)) {
int result = system(cmd.toStdString().c_str());
if (result) {
emit error(url(),act());
//proxy()->emitCurlError(m_url,m_act);
}
}
}
curlthread.h
class CurlThread : public QObject, public QRunnable
{
Q_OBJECT
public:
explicit CurlThread(QObject *parent = 0);
.....
protected:
void run();
signals:
void error(QString,QString);
...
};
curlproxy.cpp
void CurlProxy::rateRU(QVariant var1, QVariant var2) {
CurlThread* thread = new CurlThread;
thread->setAct(var1.toString());
thread->setUrl(var2.toString());
QThreadPool::globalInstance()->start(thread);
connect(thread, &CurlThread::error, this, &CurlProxy::curlError, Qt::QueuedConnection);
}
void CurlProxy::curlError(QString url, QString act) {
qDebug()<<"emit error";
qDebug()<<url;
qDebug()<<m_nonRated.count();
m_nonRated.push_back(QPair<QString,QString>(url,act));
}
curlproxy.h
class CurlProxy : public QObject
{
Q_OBJECT
public:
explicit CurlProxy(QObject *parent = 0);
Q_INVOKABLE void rateRU(QVariant , QVariant );
void emitCurlError(QString,QString);
signals:
public slots:
void curlError(QString,QString);
...
};
我的代码的主要思想是我调用CurlProxy.rateRU(...)并从该函数启动CurlThread。在CurlThread :: run我只是尝试向主线程发出信号,但有时候我遇到了崩溃。有例子日志:
[D] CurlThread::run:9 - Thread started "431376"
[D] CurlThread::run:15 - 1234
[D] CurlProxy::curlError:18 - emit error
[D] CurlProxy::curlError:19 - "rulez"
[D] CurlProxy::curlError:20 - 0
[D] CurlThread::run:17 - 5678
[D] onPressAndHold:43 - ident is 431375 index 1
[D] CurlThread::run:9 - Thread started "431375"
[D] CurlThread::run:15 - 1234
[D] CurlThread::run:17 - 5678
Application crashed