Downloading problem in QML via binding Qt



  • Hi,I want to dowload a file via Qt in QML ,after finishing dowloading the file , I deal with it in other function ,this can be described in qml function just like this .

    @
    import CDownLoadFile 1.0
    import CReadEpubFile 1.0
    ...........
    CDownLoadFile{id:epubdownload}
    CReadEpubFile{id:readepubfile}

        function bookClicked() {
        epubdownload.DownloadFile("http://s3.amazonaws.com/manybooksepub/munroeki3565235652-8epub.epub")
        readepubfile.ExtractFile("/sdcard/tmpEpubFile/munroeki3565235652-8epub.epub")
    }
    

    @

    the DownloadFile was implemented in qt like below
    @
    void CDownLoadFile::DownloadFile(QUrl url)
    {
    QDir directory("/sdcard");
    directory.mkdir(QString("tmpEpubFile"));
    directory.setCurrent("/sdcard/tmpEpubFile");
    QFileInfo fileInfo(url.path());
    QString fileName = fileInfo.fileName();
    m_pFile = new QFile(fileName);
    m_pFile->open(QIODevice::WriteOnly);
    m_pManager=new QNetworkAccessManager();
    m_pReply= m_pManager->get(QNetworkRequest(url));
    QObject::connect(m_pReply, SIGNAL(readyRead()),this, SLOT(HttpReadyRead()));
    QObject::connect(m_pReply,SIGNAL(finished()),this,SLOT(HttpFinished()));
    }
    @

    the result is that epubdownload.DownloadFile() and readepubfile.ExtractFile() are at the same time executing ,the file was not completely downloaded ,other operation to the file goes on!

    how can i resolve it!
    Thanks very much!please forgive my poor english!
    my best regards!



  • Looks like your HttpFinished() slot is called when download is finished?
    Inside this slot, raise a signal say downloadFinished() and catch it on Qml side like below

    @CDownLoadFile{
    id:epubdownload
    onDownloadFinished: {
    readepubfile.ExtractFile("/sdcard/tmpEpubFile/munroeki3565235652-8epub.epub")
    }
    }@

    Basically you are informing Qml about download finish, and then calling ExtractFile function.
    try and let us know if this works...



  • [quote author="Vijay Bhaska Reddy" date="1309234316"]Looks like your HttpFinished() slot is called when download is finished? Inside this slot, raise a signal say downloadFinished() and catch it on Qml side like below @CDownLoadFile{ id:epubdownload onDownloadFinished: { readepubfile.ExtractFile("/sdcard/tmpEpubFile/munroeki3565235652-8epub.epub") } }@ Basically you are informing Qml about download finish, and then calling ExtractFile function. try and let us know if this works...[/quote]

    it does not work ,thank you



  • when you say it does not work

    1. Is your HttpFinished() is called as soon as the file download is finished? If this is not working, everything else will fail.
    2. Are you emitting downloadFinished() signal from HttpFinished() ?
    3. Can you place a console.log() inside onDownloadFinished: and see if the control comes inside onDownloadFinished.

    Which one of these steps fail??



  • [quote author="Vijay Bhaska Reddy" date="1309241695"]when you say it does not work

    1. Is your HttpFinished() is called as soon as the file download is finished? If this is not working, everything else will fail.
    2. Are you emitting downloadFinished() signal from HttpFinished() ?
    3. Can you place a console.log() inside onDownloadFinished: and see if the control comes inside onDownloadFinished.

    Which one of these steps fail??
    [/quote]

    Thank you for reply,I think that is not the steps above,here is my HttpFinished() function
    @
    void CDownLoadFile::HttpFinished()
    {
    m_pFile->flush();
    m_pFile->close();
    m_pReply->deleteLater();
    m_pReply = 0;
    delete m_pFile;
    m_pFile = 0;
    emit DownloadFinished();
    }
    @
    the signal was defined in the header file ,just like
    @
    signals:
    Q_INVOKABLE void DownloadFinished();
    @

    In the qml file ,I invoke the signal like this
    @
    CDownLoadFile{
    id:epubdownload
    onDownloadFinished: {
    readepubfile.ExtractFile("/sdcard/tmpEpubFile/munroeki3565235652-8epub.epub")
    }
    }
    @
    The Qt creator shows that onDownloadFinished is not a valid property name,when I run the project ,the console shows that qrc:/qml/Reader/DelegateGrid.qml:13:9: Cannot assign to non-existent property "onDownloadFinished" .
    why it happens!
    thank you again!



  • couple of syntax mistakes...

    1. Name of signal should be "downloadFinished" ( should start with small letter).
    2. Don't need Q_INVOKABLE in-front of "void downloadFinished()". Remove it.

    now keep everything else same and compile the code and see if it works.

    PS: signal downloadFinished() automatically generates a slot called onDownloadFinished() in the qml side.



  • [quote author="Vijay Bhaska Reddy" date="1309253856"]couple of syntax mistakes...

    1. Name of signal should be "downloadFinished" ( should start with small letter).
    2. Don't need Q_INVOKABLE in-front of "void downloadFinished()". Remove it.

    now keep everything else same and compile the code and see if it works.

    PS: signal downloadFinished() automatically generates a slot called onDownloadFinished() in the qml side.
    [/quote]

    You are genius,it works ,now after downloading the file ,it operates the extraction
    Thanks again!
    My best regards!



  • you are most welcome..
    this link helps to teach QtQuick http://doc.qt.nokia.com/4.7/qtquick.html



  • Hi all - newbie here. How does the above solution deal with large files? I read somewhere else that reading the source in chunks and also writing in chunks is better on memory? For example 10MB, 100MB, 1GB, 4GB sizes?

    How would the above code change (if at all?).

    thanks


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.