开发者

qt networkManager get

I want to download the url entered in the line edit widget.

I am not able to get it working , can some one please give me a short code snippet which can put the values of the file to a QString ?

void imdb::on_imdbGetButton_clicked()
{

Qstring link1 = ui->lineEdit2->text();
// QString link1 is the url to be downloaded.
}

I have added , the required header files..

Thanks..开发者_开发技巧


I guess you're trying to download a file via http. Here's what you could do:

  1. In you *.pro file add QT += network
  2. Create an instance of QNetworkAccessManager class;
  3. Supply your file URL to via QNetworkRequest object: manager->get(QNetworkRequest("file_url"));
  4. Connect to the finished signal of the QNetworkAccessManager
  5. In the finished signal handler read the content of the QNetworkReply and save it to the local file.

Below is a small example. Download will start in the button click of the MainForm class:

mainwindow.h:

#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QDebug>
#include <QUrl>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QFile>
#include <QFileInfo>
#include <QPushButton>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QNetworkAccessManager* _manager;

private slots:
    void on_pushButton_clicked();
    void downloadFinished(QNetworkReply *reply);
};

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QPushButton* button = new QPushButton("Download", this);
    button->setGeometry(20, 20, 80, 30);
    connect(button, SIGNAL(clicked()), SLOT(on_pushButton_clicked()));

    _manager = new QNetworkAccessManager(this);
    connect(_manager, SIGNAL(finished(QNetworkReply*)), SLOT(downloadFinished(QNetworkReply*)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QUrl url("http://pics.mtii.com/ClassPictures2011/MIA/E110227-PMIA3-JEAN/thumbnails/P2270448%20copy.jpg");
    _manager->get(QNetworkRequest(url));
}

void MainWindow::downloadFinished(QNetworkReply *reply)
{
    QUrl url = reply->url();
    if (reply->error())
    {
        qDebug() << "Download of " <<  url.toEncoded().constData()
                 << " failed: " << reply->errorString();
    }
    else
    {
        QString path = url.path();
        QString fileName = QFileInfo(path).fileName();
        if (fileName.isEmpty()) fileName = "download";

        QFile file(fileName);
        if (file.open(QIODevice::WriteOnly))
        {
            file.write(reply->readAll());
            file.close();
        }

        qDebug() << "Download of " <<  url.toEncoded().constData()
                 << " succeded saved to: " << fileName;
    }
}

hope this helps, regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜