Results 1 to 20 of 20

Thread: Downloading a file and saving in a path

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2012
    Location
    Hyderabad
    Posts
    82
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    5

    Default Downloading a file and saving in a path

    Dear Forums,


    Hi guys i have a problem with downloading a file......Actually i need to save a file in a path which i have done but what my question is can we save the same file in two different paths is it possible at a time (or)
    is there any way to download a file i mean xml file which im generating through my code to the system path which when clicked should raise a window by showing download option.
    Please let me know if any solution.Any solution would be appreciable.Thanks in Advance...


    Regards,

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Downloading a file and saving in a path

    Assuming that downloading in this context means getting data from a QNetworkAccessManager::get() call.

    You get the data from the QNetworkReply and you can store it in as many locations as you want.

    If you open two files and write the received data into both files, it will be stored in both files.

    Cheers,
    _

  3. #3
    Join Date
    Jul 2012
    Location
    Hyderabad
    Posts
    82
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    5

    Default Re: Downloading a file and saving in a path

    Dear anda_skoa,


    Thanks for the reply..........I have got a code and i have worked on and its working fine...Please find the code below:

    Qt Code:
    1. #include "qtdownload.h"
    2. #include <QCoreApplication>
    3. #include <QUrl>
    4. #include <QNetworkRequest>
    5. #include <QFile>
    6. #include <QDebug>
    7.  
    8. QtDownload::QtDownload() : QObject(0) {
    9. QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(downloadFinished(QNetworkReply*)));
    10.  
    11. }
    12.  
    13. QtDownload::~QtDownload() {
    14.  
    15. }
    16.  
    17. void QtDownload::setTarget(const QString &t) {
    18. this->target = t;
    19. }
    20.  
    21. void QtDownload::downloadFinished(QNetworkReply *data) {
    22. QFile localFile("/home/venugopal/downloadedfile");
    23. if (!localFile.open(QIODevice::WriteOnly))
    24. return;
    25. const QByteArray sdata = data->readAll();
    26. localFile.write(sdata);
    27. qDebug() << sdata;
    28. localFile.close();
    29. // emit done();
    30. }
    31.  
    32. void QtDownload::download() {
    33. QUrl url = QUrl::fromEncoded(this->target.toLocal8Bit());
    34. QNetworkRequest request(url);
    35. QObject::connect(manager.get(request), SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
    36.  
    37. }
    38.  
    39. //void QtDownload::downloadProgress(qint64 received, qint64 total) {
    40. // qDebug() << "HI whts Received"<<received <<"HI whts total"<< total;
    41.  
    42. //}
    To copy to clipboard, switch view to plain text mode 



    Now what my doubt is for generating an xml file i am using a program and for downloading im using the other........When i try to call those methods in a single class by having everything at once....Its not allowing me rather giving me error messages......Please provide me an idea.....Any solution would be appreciable........Thanks in Advance.....


    Regards,

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: Downloading a file and saving in a path

    what error message?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Jul 2012
    Location
    Hyderabad
    Posts
    82
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    5

    Default Re: Downloading a file and saving in a path

    Dear Amleto,


    Thanks for the reply..........The Error message that i am getting is "No such slot to be defined" eventhough i have given the slot etc ........And the code that i have given is the one that i have completely worked on and its working fine but when tried to do both generating and downloading its not allowing me to do.Hope you got me now......Any solution would be appreciable.....Thanks in Advance.........Please find the attachment of both the programs for your reference....


    The below program is for generating an xml file in a location:

    Qt Code:
    1. #include "generatexml.h"
    2. #include "ui_generatexml.h"
    3. #include<QMap>
    4. #include<QFile>
    5. #include<QXmlStreamWriter>
    6. #include<QMapIterator>
    7. #include<QMessageBox>
    8. #include<QDebug>
    9. #include<QDebug>
    10. #include<QSqlDatabase>
    11. #include<QSqlError>
    12. #include<QSqlQuery>
    13. #include<qdebug.h>
    14. #include<QSqlRecord>
    15.  
    16.  
    17. GenerateXML::GenerateXML(QWidget *parent) :
    18. QMainWindow(parent),
    19. ui_xml(new Ui::GenerateXML)
    20. {
    21.  
    22. ui_xml->setupUi(this);
    23. setWindowTitle("Generate Xml");
    24.  
    25. connect(ui_xml->pushButton,SIGNAL(clicked()),this,SLOT(CreateXMLFile()));
    26.  
    27. // //**** CALLING FUNCTION *****
    28. // CreateXMLFile();
    29. // QObject::deleteLater();
    30. }
    31.  
    32. GenerateXML::~GenerateXML()
    33. {
    34. qDebug("In Destructor---------------");
    35. delete ui_xml;
    36. }
    37. void GenerateXML::CreateXMLFile()
    38. {
    39.  
    40. QFile file("/mnt/jffs2/Synch.xml");
    41. if(!file.open(QIODevice::WriteOnly))
    42. {
    43. QMessageBox::critical(this,
    44. "GenerateXML::parseXML",
    45. "Couldn't open example.xml",
    46. return;
    47.  
    48. }
    49. else
    50. {
    51. qDebug("THE FILE IS READ ONLY MODE --");
    52.  
    53. qDebug()<<"**************establishing new DB connection**************";
    54. QSqlDatabase db2 = QSqlDatabase::addDatabase("QSQLITE");
    55. db2.setDatabaseName("/mnt/jffs2/apmcdb.sqlite");
    56. bool ok1 = db2.open();
    57. qDebug()<<"*(((((((((((((((((((("<<ok1;
    58. if(ok1)
    59. {
    60. qDebug() << "Database opened";
    61. }
    62. else {
    63. qDebug() << "Database not opened";
    64. }
    65.  
    66. //*********** GETTING COLOUMN NAMES &&& DATA FROM TABLE **************
    67. QSqlQuery fardb;
    68. bool que=fardb.exec("SELECT * from something");
    69. qDebug()<<"Query Check---------------"<<que;
    70. QSqlRecord colnames=fardb.record();
    71. qDebug()<<"no of coloumnss-----------"<<colnames.count();
    72. qDebug()<<"colname----" <<colnames.fieldName(0);
    73.  
    74. //*********CREATING & WRITING TO XML FILE**********************
    75. QXmlStreamWriter* xmlWriter= new QXmlStreamWriter();
    76. xmlWriter->setDevice(&file);
    77. xmlWriter->setAutoFormatting(true);
    78. /*Start document */
    79. xmlWriter->writeStartDocument();
    80. xmlWriter->writeStartElement("MAIN");
    81. while(fardb.next()) //entering to write row of query records
    82. {
    83. //*******start tag persons******
    84. xmlWriter->writeStartElement("Record1"); //start new tag for one enitre row of record
    85. for(int cols=0;cols<colnames.count();cols++) //gets the coloumns of the particular row
    86. {
    87. //writes each coloumn in new line with <colname_tag> col_value </colname_tag>
    88.  
    89. xmlWriter->writeTextElement(colnames.fieldName(cols),fardb.value(cols).toString());
    90. }
    91. xmlWriter->writeEndElement(); //end tag for row of record
    92. //********end tag persons**********/
    93. }//ends after writing last record row of query
    94. xmlWriter->writeEndElement();
    95. xmlWriter->writeEndDocument();
    96. /*end document */
    97. delete xmlWriter;
    98. }
    99.  
    100. }
    To copy to clipboard, switch view to plain text mode 

    And the other program for downloading is as i have already given....Hope this would be a better reference...



    Regards,

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Downloading a file and saving in a path

    Quote Originally Posted by StarRocks View Post
    The Error message that i am getting is "No such slot to be defined" eventhough i have given the slot etc
    Check if you have all the necessary requirements:
    - the C++ method
    - declared in a slots section
    - in a QObject subclass
    - having the Q_OBJECT marker

    Cheers,
    _

  7. #7
    Join Date
    Jul 2012
    Location
    Hyderabad
    Posts
    82
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    5

    Default Re: Downloading a file and saving in a path

    Dear anda_skoa,


    Thanks for the reply....I have checked everything is working fine but not knowing where the problem is .......I am sending you the complete programs of both in a zip format so that you can get an idea i guess.......Please find the attachment of the files given....Any solution would be appreciable ..........Thanks in advance...


    Regards,
    Attached Files Attached Files

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Downloading a file and saving in a path

    I can't see anything wrong with your code (at least the part that declares and connects to the slot). It is possible that since your UI is named "GenerateXML" and your main window class is *also* named "GenerateXML" the SLOT macro is getting confused about the scope. Try renaming your main window class, maybe something like "GenerateXMLWindow" and see if that makes any difference.

    Are you certain that the connect() error message is due to the call in *your* code, and not coming from somewhere else? Typically the error message also includes the name of the signal / slot that can't be found, and you have not provided that information in your post.

  9. #9
    Join Date
    Jul 2012
    Location
    Hyderabad
    Posts
    82
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    5

    Default Re: Downloading a file and saving in a path

    Dear d_stranz,


    Thanks for the reply........Actually the error is now from the other project its from the same and renamed the class name as you said me to but helpless no change in the code...........I want to call the download of xml file project in the Generatingxml project because i don't want to make the code long and confused so thought to have all in a single project.........Hope you got my point and understood what i really wanted to frame the sentence in and it says the slot/signal not found but not providing me the name of the slot/signal.........Any solution would be appreciable.........Thanks in Advance......

  10. #10
    Join Date
    Jul 2012
    Location
    Hyderabad
    Posts
    82
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    5

    Default Re: Downloading a file and saving in a path

    Dear d_stranz,

    Please find the attachment of my updated program on generating and downloading........What my problem here is it downloading before i generate any file......It means downloading is done first.........The error that im getting here is "Object::connect: No such slot MainWindow::downloadProgress(qint64,qint64)
    Object::connect: (receiver name: 'MainWindow')" ............Please have a look at the code and let me know with a solution.......Any solution would be appreciable.....Thanks in Advance........


    Regards,
    Attached Files Attached Files

  11. #11
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Downloading a file and saving in a path

    You are trying to create two MainWindows, both will run simultaneously, if you want the 2nd MainWindow to wait until first one is done then
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8.  
    9. w.show();
    10. a.exec();//<<<<<<<<<<<<<<<<<<<<
    11.  
    12.  
    13. MainWindow dl;
    14. dl.setTarget("/mnt/jffs2/Synch.xml");
    15.  
    16. dl.download();
    17.  
    18. return 0;//<<<<<<<<<<<<<<<<<<<<
    19. }
    To copy to clipboard, switch view to plain text mode 

    also the error you mentioned (altually warnning) is because you commented out that solt in the code
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  12. #12
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: Downloading a file and saving in a path

    Dear StarRocks - some independence please. Don't You see in mainwindow.h that line with definiton of downloadProgress slot is commented ?

Similar Threads

  1. Concurrent file downloading
    By Alir3z4 in forum Qt Programming
    Replies: 14
    Last Post: 21st February 2012, 16:56
  2. downloading the file problems
    By migel in forum Newbie
    Replies: 0
    Last Post: 7th June 2011, 18:30
  3. Playing file with Phonon while stil downloading it.
    By alexandernst in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2011, 12:25
  4. Downloading file over https with proxy
    By szraf in forum Qt Programming
    Replies: 0
    Last Post: 5th April 2011, 17:56
  5. File size of a remote file without downloading it
    By dirkdepauw in forum Qt Programming
    Replies: 5
    Last Post: 4th November 2010, 10:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.