qt 网络管理器 获取

发布于 2024-10-21 00:06:53 字数 274 浏览 7 评论 0原文

我想下载行编辑小部件中输入的网址。

我无法让它工作,有人可以给我一个简短的代码片段,可以将文件的值放入 QString 中吗?

void imdb::on_imdbGetButton_clicked()
{

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

我已经添加了所需的头文件..

谢谢..

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛刀软妹 2024-10-28 00:06:53

我猜您正在尝试通过 http 下载文件。您可以执行以下操作:

  1. 在 *.pro 文件中添加 QT += network
  2. 创建 QNetworkAccessManager 类;
  3. 通过 QNetworkRequest 对象提供您的文件 URL:manager->get (QNetworkRequest("file_url"));
  4. 连接到已完成QNetworkAccessManager 的 信号
  5. 在完成的信号处理程序中读取 QNetworkReply 的内容并将其保存到本地文件中。

下面是一个小例子。下载将在 MainForm 类的按钮单击中开始:

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;
    }
}

希望这有帮助,问候

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文