mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-17 05:36:39 +02:00
Merge pull request #3124 from Atlante45/replace_qnetworkaccessmanager
Fixed invokeMethod unable to find method + fixed children in different t...
This commit is contained in:
commit
db5abbc363
4 changed files with 30 additions and 9 deletions
|
@ -210,12 +210,15 @@ void Agent::run() {
|
|||
|
||||
NetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
|
||||
QNetworkReply *reply = networkAccessManager.get(QNetworkRequest(scriptURL));
|
||||
QNetworkDiskCache* cache = new QNetworkDiskCache(&networkAccessManager);
|
||||
|
||||
// Make sure cache on same thread than its parent (NetworkAccessManager)
|
||||
QNetworkDiskCache* cache = new QNetworkDiskCache();
|
||||
cache->moveToThread(networkAccessManager.thread());
|
||||
cache->setParent(&networkAccessManager);
|
||||
|
||||
QString cachePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
|
||||
cache->setCacheDirectory(!cachePath.isEmpty() ? cachePath : "agentCache");
|
||||
QMetaObject::invokeMethod(&networkAccessManager, "setCache",
|
||||
Qt::BlockingQueuedConnection,
|
||||
Q_ARG(QAbstractNetworkCache*, cache));
|
||||
networkAccessManager.setCache(cache);
|
||||
|
||||
qDebug() << "Downloading script at" << scriptURL.toString();
|
||||
|
||||
|
|
|
@ -316,11 +316,14 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) :
|
|||
QString cachePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
|
||||
|
||||
NetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
|
||||
QNetworkDiskCache* cache = new QNetworkDiskCache(&networkAccessManager);
|
||||
|
||||
// Make sure cache on same thread than its parent (NetworkAccessManager)
|
||||
QNetworkDiskCache* cache = new QNetworkDiskCache();
|
||||
cache->moveToThread(networkAccessManager.thread());
|
||||
cache->setParent(&networkAccessManager);
|
||||
|
||||
cache->setCacheDirectory(!cachePath.isEmpty() ? cachePath : "interfaceCache");
|
||||
QMetaObject::invokeMethod(&networkAccessManager, "setCache",
|
||||
Qt::BlockingQueuedConnection,
|
||||
Q_ARG(QAbstractNetworkCache*, cache));
|
||||
networkAccessManager.setCache(cache);
|
||||
|
||||
ResourceCache::setRequestLimit(3);
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
//
|
||||
|
||||
#include <QMetaObject>
|
||||
#include <QAbstractNetworkCache>
|
||||
#include <QThread>
|
||||
|
||||
#include "NetworkAccessManager.h"
|
||||
|
@ -146,4 +147,15 @@ QNetworkReply* NetworkAccessManager::sendCustomRequest(const QNetworkRequest& re
|
|||
return result;
|
||||
}
|
||||
return QNetworkAccessManager::sendCustomRequest(request, verb, data);
|
||||
}
|
||||
|
||||
void NetworkAccessManager::setCache(QAbstractNetworkCache* cache) {
|
||||
if (QThread::currentThread() != thread()) {
|
||||
qRegisterMetaType<QAbstractNetworkCache*>();
|
||||
QMetaObject::invokeMethod(this,
|
||||
"setCache",
|
||||
Qt::QueuedConnection,
|
||||
Q_ARG(QAbstractNetworkCache*, cache));
|
||||
}
|
||||
QNetworkAccessManager::setCache(cache);
|
||||
}
|
|
@ -18,7 +18,9 @@
|
|||
|
||||
/// Wrapper around QNetworkAccessManager wo that we only use one instance
|
||||
/// For any other method you should need, make sure to be on the right thread
|
||||
/// or call the method using QMetaObject::invokeMethod()
|
||||
/// or if it is not but is a slot, use QMetaObject::invokeMethod()
|
||||
/// In the case what you want to call isn't a slot and you aren't on the same thread,
|
||||
/// then add then method to the method to the wrapper with the Q_INVKABLE flag
|
||||
class NetworkAccessManager : public QNetworkAccessManager {
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
@ -33,6 +35,7 @@ public:
|
|||
Q_INVOKABLE QNetworkReply* put(const QNetworkRequest& request, QHttpMultiPart* multiPart);
|
||||
Q_INVOKABLE QNetworkReply* put(const QNetworkRequest& request, const QByteArray& data);
|
||||
Q_INVOKABLE QNetworkReply* sendCustomRequest(const QNetworkRequest& request, const QByteArray& verb, QIODevice* data = 0);
|
||||
Q_INVOKABLE void setCache(QAbstractNetworkCache* cache);
|
||||
|
||||
private:
|
||||
NetworkAccessManager();
|
||||
|
|
Loading…
Reference in a new issue