Qt6 Windows slower than Qt6 Ubuntu
-
@Joe-von-Habsburg said in Qt6 Windows slower than Qt6 Ubuntu:
You suggested to me "readyRead" signal but now, I could not readyAll because, incomplated data is received
As already suggested: you need to accumulate incoming data in a buffer. readyRead() signal can be emited several times until you get everything.
"Do you mean say "finished" signal ?" - yes
https://doc.qt.io/qt-6/qnetworkreply.html#finished
It even explains: "In particular, if no calls to read() were made as a result of readyRead(), a call to readAll() will retrieve the full contents in a QByteArray." -
@jsulm said in Qt6 Windows slower than Qt6 Ubuntu:
As already suggested: you need to accumulate incoming data in a buffer. readyRead() signal can be emited several times until you get everything.
Hmm now I should not assign reply->readAll(), I should append :). I will try it.
_sample.append(_replySample->readAll());
@jsulm said in Qt6 Windows slower than Qt6 Ubuntu:
"Do you mean say "finished" signal ?" - yes
I cannot use it because memory leak.
-
@Joe-von-Habsburg
As @jsulm has said. WithQNetworkAccessManager::setAutoDeleteReplies(true)
set you should be able to use either of the following approaches:QByteArray _bytesRead; // class member variable _bytesRead.clear(); connect(_reply, &QNetworkReply::finished, this, &Class::onFinished); _reply = _manager.get(_request); void Class::onFinished() { _bytesRead = _reply->readAll(); // read all bytes in one go at the end, just before `_reply` gets auto-deleted }
or
QByteArray _bytesRead; // class member variable _bytesRead.clear(); connect(_reply, &QNetworkReply::readyRead, this, &Class::onReadyRead); connect(_reply, &QNetworkReply::finished, this, &Class::onFinished); _reply = _manager.get(_request); void Class::onReadyRead() { _bytesRead += _reply->readAll(); // *append* this time's bytes read to buffer } void Class::onFinished() { // I think `_bytesRead` should contain all data by now, when reply has finished // If not call `_bytesRead += _reply->readAll();` or `onReadyRead()` one last time }
[Edit: Fixed code highlighting ~kshegunov]
-
@Joe-von-Habsburg said in Qt6 Windows slower than Qt6 Ubuntu:
cannot use it because memory leak.
There should not be memory leak unless your code is wrong.
Simply connect a slot to finished() signal and in that slot call readAll() and deleteLater() on the reply. -
@JonB said in Qt6 Windows slower than Qt6 Ubuntu:
QByteArray _bytesRead; // class member variable
_bytesRead.clear();
connect(_reply, &QNetworkReply::readyRead, this, &Class::onReadyRead);
connect(_reply, &QNetworkReply::finished, this, &Class::onFinished);
_reply = _manager.get(_request);void Class::onReadyRead()
{
_bytesRead += _reply->readAll(); // append this time's bytes read to buffer
}void Class::onFinished()
{
// I think_bytesRead
should contain all data by now, when reply has finished
// If not call_bytesRead += _reply->readAll();
oronReadyRead()
one last time
}its work !!! Thank you so much
-
-
@Joe-von-Habsburg
Do your timings/memory consumption with this asynchronous approach (i.e. no_loop.exec()
) and not with that originalwhile()
loop. (If you want to run it more than once, after you getfinished()
on one start the next one there or on aQTimer::singleShot()
.) See whether you still get bad performance on one versus the other. -
Last edit my code :
DataReceiver::DataReceiver(QObject *parent) : QObject{parent} { _manager.setAutoDeleteReplies(true); } void DataReceiver::start() { _connection++; if(_connection > 1) return; _takeData = true; run(); } void DataReceiver::stop() { _takeData = false; _connection = 0; _data.clear(); } void DataReceiver::getData() { _data.clear(); QString url = QString("http://localhost:%1/sample").arg(_port); QUrl _apiUrl(url); QNetworkRequest _request(_apiUrl); _reply = _manager.get(_request); connect(_reply, &QNetworkReply::readyRead, this, &DataReceiver::onReadReady); connect(_reply, &QNetworkReply::finished, this, &DataReceiver::onFinished); } void DataReceiver::run() { if(!_takeData) return; getData(); } void DataReceiver::onReadReady() { _data += _reply->readAll(); } void DataReceiver::onFinished() { emit sendData(_data); run(); }
its working. time slow down from 65ms to 165ms but its working.