Results 1 to 17 of 17

Thread: [QT 5.0] Downloading from a URL then saving to client-defined path/filename?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: [QT 5.0] Downloading from a URL then saving to client-defined path/filename?

    Two other things:

    - you don't need a new QNetworkManager for every request, just one will do fine (can handle multiple requests in parallel)
    - your code becomes the owner of the returned QNetworkReply so your code has to delete it. The easiest way to do that is to call reply->deleteLater() in the finished slot.

    Cheers,
    _

  2. #2
    Join Date
    Jan 2013
    Location
    Temecula, CA
    Posts
    19
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [QT 5.0] Downloading from a URL then saving to client-defined path/filename?

    So if I put the following code where QMainWindow function is it won't have to make a new one every time? Would this help with performance?
    EDIT: What if I set it as a public class definition? I have QNetworkAccessManager *manager; but the program crashes when I try to send the request. (When I press the pushButton originally set to send it)
    Qt Code:
    1. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    To copy to clipboard, switch view to plain text mode 

    Here's my complete .cpp file for reference:
    Qt Code:
    1. #include "minecraftskindownloader.h"
    2. #include "ui_minecraftskindownloader.h"
    3. #include "nouser.h"
    4. #include <QWidget>
    5. #include <QObject>
    6. #include <QtNetwork/QNetworkAccessManager>
    7. #include <QFileDialog>
    8. #include <QtNetwork/QNetworkRequest>
    9. #include <QDebug>
    10. #include <QUrl>
    11. #include <QFile>
    12. #include <QPixmap>
    13. #include <QMessageBox>
    14.  
    15. MinecraftSkinDownloader::MinecraftSkinDownloader(QWidget *parent) :
    16. QMainWindow(parent),
    17. ui(new Ui::MinecraftSkinDownloader)
    18. {
    19. ui->setupUi(this);
    20. this->setWindowTitle("Minecraft Skin Downloader");
    21. setFixedSize(400, 200);
    22. setStyleSheet("QMainWindow { background-image: url(:/bg.png); } ");
    23.  
    24. connect(ui->save, SIGNAL(clicked()), this, SLOT(onsaveAs_clicked()));
    25. }
    26.  
    27. MinecraftSkinDownloader::~MinecraftSkinDownloader()
    28. {
    29. delete ui;
    30. }
    31.  
    32. //http://s3.amazonaws.com/MinecraftSkins/USERNAMEHERE.png
    33.  
    34. void MinecraftSkinDownloader::on_pushButton_clicked()
    35. {
    36. QString prefix = "http://s3.amazonaws.com/MinecraftSkins/";
    37. QString usrl = prefix += ui->lineEdit->text() += ".png";
    38.  
    39. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    40. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slot_netwManagerFinished(QNetworkReply*)));
    41.  
    42. QUrl url(usrl);
    43. QNetworkRequest request(url);
    44.  
    45. manager->get(request);
    46.  
    47. }
    48.  
    49. void MinecraftSkinDownloader::slot_netwManagerFinished(QNetworkReply *reply)
    50. {
    51.  
    52. if (reply->error() != QNetworkReply::NoError)
    53. {
    54. qDebug() << "Error in" << reply->url() << ":" << reply->errorString();
    55. NoUser mDiag;
    56. mDiag.setModal(true);
    57. mDiag.exec();
    58. return;
    59. }
    60.  
    61. MinecraftSkinDownloader::pngData = reply->readAll();
    62. QPixmap pixmap;
    63. pixmap.loadFromData(MinecraftSkinDownloader::pngData);
    64. QPixmap scaled = pixmap.scaled(128,64,Qt::IgnoreAspectRatio, Qt::FastTransformation);
    65. ui->preview->show();
    66. ui->preview->setStyleSheet("QLabel { background-color: white; }");
    67. ui->preview->setPixmap(scaled);
    68. reply->deleteLater();
    69.  
    70. }
    71.  
    72. void MinecraftSkinDownloader::onsaveAs_clicked()
    73. {
    74. QString filename = QFileDialog::getSaveFileName(this, tr("Save Skin"), "C:/Users/", ("PNG Image (*.png)"));
    75.  
    76. QFile file(filename);
    77. file.open(QIODevice::WriteOnly);
    78. file.write(MinecraftSkinDownloader::pngData);
    79. file.close();
    80. }
    81.  
    82. void MinecraftSkinDownloader::on_pushButton_2_clicked()
    83. {
    84. ui->lineEdit->clear();
    85. ui->preview->hide();
    86. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Salads; 23rd January 2013 at 07:35.

  3. #3
    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: [QT 5.0] Downloading from a URL then saving to client-defined path/filename?

    Quote Originally Posted by Salads View Post
    [COLOR="#D3D3D3"]
    EDIT: What if I set it as a public class definition? I have QNetworkAccessManager *manager; but the program crashes when I try to send the request. (When I press the pushButton originally set to send it)
    I am not sure what you mean with that, the QNetworkManager creation in the code you posted seems to be unchanged.

    Btw, you'll also have to deleted the network reply in the error handling case

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    Salads (23rd January 2013)

  5. #4
    Join Date
    Jan 2013
    Location
    Temecula, CA
    Posts
    19
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [QT 5.0] Downloading from a URL then saving to client-defined path/filename?

    Got the error part thanks

    I'll update my code:

    .h File:
    Qt Code:
    1. #ifndef MINECRAFTSKINDOWNLOADER_H
    2. #define MINECRAFTSKINDOWNLOADER_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtNetwork/QNetworkReply>
    6. #include <QtNetwork/QNetworkAccessManager>
    7.  
    8. namespace Ui {
    9. class MinecraftSkinDownloader;
    10. }
    11.  
    12. class MinecraftSkinDownloader : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MinecraftSkinDownloader(QWidget *parent = 0);
    18. ~MinecraftSkinDownloader();
    19. QByteArray pngData;
    20. QNetworkAccessManager *manager;
    21.  
    22. private slots:
    23.  
    24. void slot_netwManagerFinished(QNetworkReply *reply);
    25.  
    26. void onsaveAs_clicked();
    27.  
    28. void on_clear_clicked();
    29.  
    30. void on_pushButton_clicked();
    31.  
    32. private:
    33. Ui::MinecraftSkinDownloader *ui;
    34.  
    35.  
    36. };
    37.  
    38. #endif // MINECRAFTSKINDOWNLOADER_H
    To copy to clipboard, switch view to plain text mode 

    .cpp File
    Qt Code:
    1. #include "minecraftskindownloader.h"
    2. #include "ui_minecraftskindownloader.h"
    3. #include <QWidget>
    4. #include <QObject>
    5. #include <QtNetwork/QNetworkAccessManager>
    6. #include <QFileDialog>
    7. #include <QtNetwork/QNetworkRequest>
    8. #include <QDebug>
    9. #include <QUrl>
    10. #include <QFile>
    11. #include <QPixmap>
    12. #include <QMessageBox>
    13.  
    14. MinecraftSkinDownloader::MinecraftSkinDownloader(QWidget *parent) :
    15. QMainWindow(parent),
    16. ui(new Ui::MinecraftSkinDownloader)
    17. {
    18. ui->setupUi(this);
    19. this->setWindowTitle("Minecraft Skin Downloader");
    20. QIcon icon(":/ico");
    21. setWindowIcon(icon);
    22. setFixedSize(400, 200);
    23. setStyleSheet("QMainWindow { background-image: url(:/bg.png); } ");
    24.  
    25. connect(ui->save, SIGNAL(clicked()), this, SLOT(onsaveAs_clicked()));
    26.  
    27. }
    28.  
    29. MinecraftSkinDownloader::~MinecraftSkinDownloader()
    30. {
    31. delete ui;
    32. }
    33.  
    34. //http://s3.amazonaws.com/MinecraftSkins/USERNAMEHERE.png
    35.  
    36. void MinecraftSkinDownloader::on_pushButton_clicked()
    37. {
    38. QString prefix = "http://s3.amazonaws.com/MinecraftSkins/";
    39. QString usrl = prefix += ui->lineEdit->text() += ".png";
    40.  
    41. //QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    42. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slot_netwManagerFinished(QNetworkReply*)));
    43.  
    44. QUrl url(usrl);
    45. QNetworkRequest request(url);
    46.  
    47. manager->get(request);
    48.  
    49. }
    50.  
    51. void MinecraftSkinDownloader::slot_netwManagerFinished(QNetworkReply *reply)
    52. {
    53.  
    54. if (reply->error() != QNetworkReply::NoError)
    55. {
    56. qDebug() << "Error in" << reply->url() << ":" << reply->errorString();
    57. QPixmap noUsr(":/voidUser.png"); // Void User path
    58. ui->preview->show();
    59. ui->preview->setStyleSheet("background: transparent;");
    60. ui->preview->setPixmap(noUsr);
    61. reply->deleteLater();
    62. return;
    63. }
    64.  
    65. pngData = reply->readAll();
    66. QPixmap pixmap;
    67. pixmap.loadFromData(pngData);
    68. QPixmap scaled = pixmap.scaled(128,64,Qt::IgnoreAspectRatio, Qt::FastTransformation);
    69. ui->preview->show();
    70. ui->preview->setStyleSheet("QLabel { background-color: white; }");
    71. ui->preview->setPixmap(scaled);
    72. reply->deleteLater();
    73.  
    74. }
    75.  
    76. void MinecraftSkinDownloader::onsaveAs_clicked()
    77. {
    78. if(pngData.isEmpty())
    79. {
    80. ui->preview->show();
    81. QPixmap error(":/noSaveData.png");
    82. ui->preview->setPixmap(error);
    83. return;
    84. }
    85.  
    86. QString filename = QFileDialog::getSaveFileName(this, tr("Save Skin"), "C:/Users/", ("PNG Image (*.png)"));
    87.  
    88. QFile file(filename);
    89. file.open(QIODevice::WriteOnly);
    90. file.write(pngData);
    91. file.close();
    92. }
    93.  
    94. void MinecraftSkinDownloader::on_clear_clicked()
    95. {
    96. pngData.clear();
    97. ui->preview->setStyleSheet("background: transparent;");
    98. ui->lineEdit->clear();
    99. ui->preview->hide();
    100. return;
    101. }
    To copy to clipboard, switch view to plain text mode 

    It crashes the moment I press that on_pushButton_clicked(), and doesn't even get to display the error message (voidUser.png) when I leave it blank. Same thing happens when I put a valid username in there too.

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: [QT 5.0] Downloading from a URL then saving to client-defined path/filename?

    If you ran the program in your debugger you would know exactly which line your program was crashing on (probably 42, maybe 47), which is more useful than the "somewhere in on_pushButton_clicked()" level of information you have now.

    You have made a pointer to QNetworkAccessManager member variable but you never initialise that pointer. When you subsequently use it... BOOM! You need to move the equivalent of line 41 (also 42) into the constructor. A good habit to get into is: any time you add a member variable to a class immediately add an initialisation of the variable into the constructor. In the case of pointers that can be setting it to 0 or allocating a new object; either way it forces you to think about where it should be set.

  7. The following user says thank you to ChrisW67 for this useful post:

    Salads (31st January 2013)

Similar Threads

  1. Downloading a file and saving in a path
    By StarRocks in forum Qt Programming
    Replies: 19
    Last Post: 3rd January 2013, 06:43
  2. Replies: 6
    Last Post: 3rd December 2012, 07:26
  3. Full path and filename of a Qt Plugin at runtime
    By be-noix in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2011, 15:38
  4. Replies: 8
    Last Post: 17th October 2009, 08:10
  5. Replies: 2
    Last Post: 19th May 2009, 09:30

Tags for this Thread

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.