June 28, 2011

xiazhouquan xiazhouquan
Lab Rat
10 posts

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 .

  1. import CDownLoadFile 1.0
  2. import CReadEpubFile 1.0
  3. ...........
  4.     CDownLoadFile{id:epubdownload}
  5.     CReadEpubFile{id:readepubfile}
  6.    
  7.         function bookClicked() {
  8.         epubdownload.DownloadFile("http://s3.amazonaws.com/manybooksepub/munroeki3565235652-8epub.epub")
  9.         readepubfile.ExtractFile("/sdcard/tmpEpubFile/munroeki3565235652-8epub.epub")
  10.     }

the DownloadFile was implemented in qt like below

  1. void  CDownLoadFile::DownloadFile(QUrl url)
  2. {
  3.     QDir directory("/sdcard");
  4.     directory.mkdir(QString("tmpEpubFile"));
  5.     directory.setCurrent("/sdcard/tmpEpubFile");
  6.     QFileInfo fileInfo(url.path());
  7.     QString fileName = fileInfo.fileName();
  8.     m_pFile = new QFile(fileName);
  9.     m_pFile->open(QIODevice::WriteOnly);
  10.     m_pManager=new QNetworkAccessManager();
  11.     m_pReply= m_pManager->get(QNetworkRequest(url));
  12.     QObject::connect(m_pReply, SIGNAL(readyRead()),this, SLOT(HttpReadyRead()));
  13.     QObject::connect(m_pReply,SIGNAL(finished()),this,SLOT(HttpFinished()));
  14. }

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!

 Signature 

业精于勤荒于嬉,行成于思毁于随

8 replies

June 28, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
383 posts

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

  1. CDownLoadFile{
  2.        id:epubdownload
  3.        onDownloadFinished: {
  4.            readepubfile.ExtractFile("/sdcard/tmpEpubFile/munroeki3565235652-8epub.epub")
  5.        }
  6.  }

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

June 28, 2011

xiazhouquan xiazhouquan
Lab Rat
10 posts
Vijay Bhaska Reddy wrote:
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
  1. 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…

it does not work ,thank you

 Signature 

业精于勤荒于嬉,行成于思毁于随

June 28, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
383 posts

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

June 28, 2011

xiazhouquan xiazhouquan
Lab Rat
10 posts

Vijay Bhaska Reddy wrote:
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??

Thank you for reply,I think that is not the steps above,here is my HttpFinished() function

  1. void CDownLoadFile::HttpFinished()
  2. {
  3.     m_pFile->flush();
  4.     m_pFile->close();
  5.     m_pReply->deleteLater();
  6.     m_pReply = 0;
  7.     delete m_pFile;
  8.     m_pFile = 0;
  9.     emit DownloadFinished();
  10. }

the signal was defined in the header file ,just like
  1. signals:
  2.    Q_INVOKABLE  void DownloadFinished();

In the qml file ,I invoke the signal like this

  1.     CDownLoadFile{
  2.         id:epubdownload
  3.         onDownloadFinished: {
  4.             readepubfile.ExtractFile("/sdcard/tmpEpubFile/munroeki3565235652-8epub.epub")
  5.         }
  6.     }

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!

 Signature 

业精于勤荒于嬉,行成于思毁于随

June 28, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
383 posts

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.

June 28, 2011

xiazhouquan xiazhouquan
Lab Rat
10 posts

Vijay Bhaska Reddy wrote:
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.

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

 Signature 

业精于勤荒于嬉,行成于思毁于随

June 28, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
383 posts

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

November 26, 2012

gurpal2000 gurpal2000
Lab Rat
5 posts

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

 
  ‹‹ [Solved] Particular radius      Qwidgets in QML ››

You must log in to post a reply. Not a member yet? Register here!