Pergunta

I have a simple Qt application that sends files from a folder to a server.

When there is no internet connection, the expected behavior would be to save the file for later in an "offline" folder. Then, once a couple of minutes the application itself (in a different thread) would have to check that folder to see if some files await being sent.

Here's some code, I stripped some unnecessary lines:

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:

Header:

class Harvester : public QThread
{
    Q_OBJECT
public:
    QNetworkAccessManager * m_network;
    Harvester(QNetworkAccessManager * m_network);
...
}

Source:

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

Here's the error I receive:

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)

I guess the QNetworkAccessManager is conflicting when trying to send a request from another thread, even if I use the same pointer.

One other solution that I did not tried yet would be to run harvester as a separate process, but first I'd like to solve it by threading.

Thanks

Foi útil?

Solução

qt thread with movetothread

http://crpppc19.epfl.ch/doc/qt4-doc-html/html/qthread.html#details

http://crpppc19.epfl.ch/doc/qt4-doc-html/html/qobject.html#moveToThread

Try this:

Harvester::Harvester(QNetworkAccessManager * m)
{
    qDebug() << "harvie start" << endl;
    this->m_network = m;

    m_network->moveToThread(this); // Change thread affinity!
}

Hope that helps.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top