我有一个简单的Qt应用程序,可以将文件从文件夹发送到服务器。
当没有互联网连接时,预期的行为是将文件保存到以后的“离线”文件夹中。然后,几分钟后,应用程序本身(在不同的线程中)将必须检查该文件夹以查看是否有一些文件等待发送。
这是一些代码,我删除了一些不必要的行:
main.cpp中:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWin win;
win.show();
return app.exec();
}
mainwin.cpp:
MainWin::MainWin(QWidget * parent) : QWebView(parent)
{
m_network = new QNetworkAccessManager(this);
m_communicationHandler = new TestNetwork(this, m_network);
// harvie
Harvester * harvie = new Harvester(m_network);
harvie->start();
}
harvester.cpp:
部首:
class Harvester : public QThread
{
Q_OBJECT
public:
QNetworkAccessManager * m_network;
Harvester(QNetworkAccessManager * m_network);
...
}
来源:
Harvester::Harvester(QNetworkAccessManager * m)
{
qDebug() << "harvie start" << endl;
this->m_network = m;
}
...
bool Harvester::sendFile(QFileInfo *fileInfo) {
connect(this->m_network,SIGNAL(finished(QNetworkReply*)),this,SLOT(validateReply(QNetworkReply*)));
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/xml")); // @todo test
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"someFile\"; filename=\"" + fileInfo->baseName() + ".xml\""));
QFile *file = new QFile(fileInfo->filePath());
file->open(QIODevice::ReadOnly);
filePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(filePart);
qDebug() << "post" << endl;
QNetworkReply *reply = this->m_network->post(QNetworkRequest(QUrl("XXX")), multiPart);
multiPart->setParent(reply); // delete the multiPart with the reply
return true;
}
这是我收到的错误:
Warning: QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNetworkAccessManager(0x3e46c30), parent's thread is QThread(0x3dd6d50), current thread is Harvester(0x3e47eb8)
我猜QNetworkAccessManager在尝试从另一个线程发送请求时是冲突的,即使我使用相同的指针。
我还没有尝试的另一个解决方案是将收割机作为一个单独的流程运行,但首先我想通过线程来解决它。
由于
答案 0 :(得分:2)
http://crpppc19.epfl.ch/doc/qt4-doc-html/html/qthread.html#details
http://crpppc19.epfl.ch/doc/qt4-doc-html/html/qobject.html#moveToThread
试试这个:
Harvester::Harvester(QNetworkAccessManager * m)
{
qDebug() << "harvie start" << endl;
this->m_network = m;
m_network->moveToThread(this); // Change thread affinity!
}
希望有所帮助。