[Interest] Curious QNetworkAccessManager behavior.
Tony Rietwyk
tony at rightsoft.com.au
Fri Nov 1 02:16:04 CET 2019
Hi Jason,
You should only connect to QNetworkAccessManager::finished once - before
sending any files. You are adding another connection with each file.
From https://doc.qt.io/qt-5/qobject.html:
By default, a signal is emitted for every connection you make; two
signals are emitted for duplicate connections. You can break all of
these connections with a single disconnect
<https://doc.qt.io/qt-5/qobject.html#disconnect>() call. If you pass the
Qt::UniqueConnection
<https://doc.qt.io/qt-5/qt.html#ConnectionType-enum> /type/, the
connection will only be made if it is not a duplicate. If there is
already a duplicate (exact same signal to the exact same slot on the
same objects), the connection will fail and connect will return an
invalid QMetaObject::Connection
<https://doc.qt.io/qt-5/qmetaobject-connection.html>.
Regards, Tony
On 1/11/2019 11:21 am, Jason H wrote:
> I'm uploading files with QNAM.
> QFile *file = new QFile(QString("%1/%2/%3").arg(_dataDir).arg(SERVER_SYNC_DIR).arg(filename));
> if (file->open(QIODevice::ReadOnly)) {
> QNetworkRequest req(QUrl(QString("%1/%2/%3").arg(_serverUrl.toString()).arg("post").arg(filename)));
> req.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
> QNetworkReply *reply = _nam.post(req, file);
> _uploads[reply] = file;
> connect(&_nam, &QNetworkAccessManager::finished, this, &ServerSyncFolder::handlePostFinished);
>
>
> void ServerSyncFolder::handlePostFinished(QNetworkReply *reply) {
> qDebug() << Q_FUNC_INFO << reply->url() << reply->errorString();
> if (_uploads.contains(reply)) {
> _uploads[reply]->close();
> _uploads[reply]->remove();
> _uploads[reply]->deleteLater();
> _uploads.remove(reply);
>
> }
> reply->deleteLater();
> }
> Every time I upload a file, the handlePostFinished gets calle once for each file I've uploaded. For example, upload files 1.jpg, 2.jpg, 3.jpg, 4.jpg:
>
> void ServerSyncFolder::handleDirectoryChanged(const QString &) "/var/mobile/Containers/Data/Application/8787E7C7-0940-4834-B6D5-1DF049AE4BEE/Documents/sync"
> // will now post my file, after post is complete I get:
> void ServerSyncFolder::handlePostFinished(QNetworkReply *) QUrl("http://10.12.0.37:57201/post/4.jpg") "Unknown error"
> void ServerSyncFolder::handlePostFinished(QNetworkReply *) QUrl("http://10.12.0.37:57201/post/4.jpg") "Unknown error"
> void ServerSyncFolder::handlePostFinished(QNetworkReply *) QUrl("http://10.12.0.37:57201/post/4.jpg") "Unknown error"
> void ServerSyncFolder::handlePostFinished(QNetworkReply *) QUrl("http://10.12.0.37:57201/post/4.jpg") "Unknown error"
>
> Server logs do not show that I am posting it repeatedly. It is calling my slot with the last URL it posted to, for as many files as I've uploaded so far.
>
> Anyone have an idea why?
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20191101/fc6687f9/attachment.html>
More information about the Interest
mailing list