Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Using a timer in qml, the signal of downloading a file is emitted multiple times...

Using a timer in qml, the signal of downloading a file is emitted multiple times...

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cochleas.devel
    wrote on last edited by
    #1

    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: true

    onTriggered: {
        console.debug( "-----&gt; tick", Date().toString() );
        fileToDownload.downloadFile( fileToDownload.fileUrl &#41;;
    }
    

    }@

    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!

    1 Reply Last reply
    0
    • N Offline
      N Offline
      nick85
      wrote on last edited by
      #2

      Try using Qt::UniqueConnection

      @connect( &m_WebCtrl, SIGNAL(finished(QNetworkReply*)), SLOT(fileDownloaded(QNetworkReply*)), Qt::UniqueConnection );@

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        1
        • C Offline
          C Offline
          cochleas.devel
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cochleas.devel
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              What you are doing the right approach. Continue with that.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              1
              • C Offline
                C Offline
                cochleas.devel
                wrote on last edited by
                #7

                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???

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved