Using a timer in qml, the signal of downloading a file is emitted multiple times...
-
Hi everyone,
I have created a FileDownloader class, as those that exist on the web already:
Here are some lines from the *.cpp file:
@void FileDownloader::downloadFile( QUrl url )
{
connect( &m_WebCtrl, SIGNAL(finished(QNetworkReply*)), SLOT(fileDownloaded(QNetworkReply*)) );QNetworkRequest request( url ); m_WebCtrl.get( request );
}
QByteArray FileDownloader::downloadedData() const
{
return m_DownloadedData;
}void FileDownloader::fileDownloaded(QNetworkReply *pReply)
{
m_DownloadedData = pReply->readAll();pReply->deleteLater(); // emit a signal qDebug() << "emit downloaded"; emit downloaded();
}@
Then I created a Timer and a FileDownloader object in qml:
@FileDownloader {
id: fileToDownload
fileUrl: "http://www.example.com/file.txt"onDownloaded: { console.debug("onDownloaded..."); }
}
Timer {
id: timer
interval: 3000
repeat: true
running: trueonTriggered: { console.debug( "-----> tick", Date().toString() ); fileToDownload.downloadFile( fileToDownload.fileUrl ); }
}@
However I have the following problem: The first time that the timer is triggered the code works ok,
but the second, third, etc time the signal is emitted multiple times. Each time once more than the previous.
Here is the text from the log window:@qml: -----> tick 17:31:38
downloadFile
emit downloaded
qml: onDownloaded...
qml: -----> tick 17:31:41
downloadFile
emit downloaded
qml: onDownloaded...
emit downloaded
qml: onDownloaded...
qml: -----> tick 17:31:44
downloadFile
emit downloaded
qml: onDownloaded...
emit downloaded
qml: onDownloaded...
emit downloaded
qml: onDownloaded...
qml: -----> tick 17:31:47
downloadFile
emit downloaded
qml: onDownloaded...
emit downloaded
qml: onDownloaded...
emit downloaded
qml: onDownloaded...
emit downloaded
qml: onDownloaded...
@
any help, idea?Thanx in advance!
-
Try using Qt::UniqueConnection
@connect( &m_WebCtrl, SIGNAL(finished(QNetworkReply*)), SLOT(fileDownloaded(QNetworkReply*)), Qt::UniqueConnection );@
-
Everytime the timer is triggered, you are calling downloadfile. Inside the downloadfile function you are calling the connect statement. With every timer expiry, connect is called. It is giving your ripple effect. Connect needs to be called only once. Move this connect statement outside the downloadfile function and ensure that connect is done only once. if you don't have any other option call Connect with 5th parameter Qt::UniqueConnection. This will fix your issue.
-
Guys thank you very much for your help!!! I tried both! The Qt::UniqueConnection fixes the issue, however, I decided that it would be better to move the connect() function in the constructor:
@FileDownloader::FileDownloader(QObject parent) :
QObject(parent)
{
connect( &m_WebCtrl, SIGNAL(finished(QNetworkReply)), SLOT(fileDownloaded(QNetworkReply*)) );
}@Thanks again for your great assistance!
-
I have another question:
What is the best method to read (re-download and re-read) an updated file from the server? Should I do something specific?
Now if I modify the file on the server ( fileUrl: "http://www.example.com/file.txt") and save it again, while the app is running, I don't get the modified content.
What am I doing wrong?
-
What you are doing the right approach. Continue with that.
-
Thanks.
But is QNetworkAccessManager using somehow cache to store the file? Because it always reads the same content although it has changed in the meantime... Is there a way to clear cache? Or sth similar???