qt networkManager get

◇◆丶佛笑我妖孽 提交于 2019-12-13 12:46:00

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/5233057/qt-networkmanager-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!