Results 1 to 20 of 20

Thread: Downloading a file and saving in a path

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

    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
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    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
    Thanks
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    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
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    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
    Thanks
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    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
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    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
    Thanks
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    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,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    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
    Thanks
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    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
    Thanks
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    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
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    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,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    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 ?

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

    Red face Re: Downloading a file and saving in a path

    Dear Santosh,


    Thanks for the reply.........Actually i dont need that slot to be used so commented it.....As u have given the main method i have done the same but what i need is when the generation of Xml is done right after that i want it to get downloaded in the path where i have mentioned ....And download should be done after the file is generated.........Hope you got me.....Thanks in Advance.........Any solution would be appreciable........



    Regards,

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

    Default Re: Downloading a file and saving in a path

    Creating two instance of MainWindow will not help you.

    You need to have two slots connected to QPushButton click signal
    1. First slot will save the file
    2. Second slot will download it form there to new location

    Signal & Slot connection (connect() call) order is important here.
    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.

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

    Default Re: Downloading a file and saving in a path

    Dear Santosh,


    Thanks for the reply.........As you have seen the code i am pretty confused how to have the slots there .......So can you help me out in having the slots set there hope you understood my point.............As its not allowing me to have couple of slots as used by me.........Thanks in advance.........Any solution would be appreciable.....



    Regards,

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

    Default Re: Downloading a file and saving in a path

    Check this
    Attached Files Attached Files
    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.

  17. The following user says thank you to Santosh Reddy for this useful post:

    StarRocks (2nd January 2013)

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

    Default Re: Downloading a file and saving in a path

    Dear Santosh,

    Thanks for the reply........The coding that you have given is working perfectly thankyou for the code........Thanks in Advance.........


    Regards,

  19. #18
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Downloading a file and saving in a path

    @Santosh: If I can offer one nit-pick criticism - the signal / slot connections in the MainWindow constructor rely on some Qt behaviour which may not be obvious to the OP, namely that signals are handled in the order that the connections are made.

    I would have defined a signal for the MainWindow: createXMLDone() or something like that, and connected that to the slot that creates the file copy. That would also allow some degree of control - as the code is now implemented, the copy is attempted no matter what happens in the createXMLFile() method, even when it fails and there is no xml file to copy.

    Otherwise, your patience in dealing with this poster is amazing.

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

    Default Re: Downloading a file and saving in a path

    Dear d_stranz,


    Thanks for the comments but the way we have coded looks fine i guess.......And i have coded many things in the program but was lacking with somethings so Santosh helped me out in clearing those am i right santosh.......Hope you help me out in those critical solving issues............Thanks in Advance..........
    Any comments would be appreciable....


    Regards,

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

    Default Re: Downloading a file and saving in a path

    @Santosh: If I can offer one nit-pick criticism - the signal / slot connections in the MainWindow constructor rely on some Qt behaviour which may not be obvious to the OP, namely that signals are handled in the order that the connections are made.
    I agree.

    I would have defined a signal for the MainWindow: createXMLDone() or something like that, and connected that to the slot that creates the file copy. That would also allow some degree of control - as the code is now implemented, the copy is attempted no matter what happens in the createXMLFile() method, even when it fails and there is no xml file to copy.
    This is a better solution, and I would also prefer this way.
    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.

Similar Threads

  1. Concurrent file downloading
    By Alir3z4 in forum Qt Programming
    Replies: 14
    Last Post: 21st February 2012, 15:56
  2. downloading the file problems
    By migel in forum Newbie
    Replies: 0
    Last Post: 7th June 2011, 17:30
  3. Playing file with Phonon while stil downloading it.
    By alexandernst in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2011, 11:25
  4. Downloading file over https with proxy
    By szraf in forum Qt Programming
    Replies: 0
    Last Post: 5th April 2011, 16:56
  5. File size of a remote file without downloading it
    By dirkdepauw in forum Qt Programming
    Replies: 5
    Last Post: 4th November 2010, 09: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.