下载txt文件使用qtnetwork

huangapple go评论80阅读模式
英文:

Downloading txt file with qtnetwork

问题

以下是您提供的代码的翻译部分:

我正在尝试在QT中从URL下载txt文件,但似乎无法使其工作。
我遵循了这个指南 https://wiki.qt.io/Download_Data_from_URL。我完全按照指南中的方法实现了filedownloader类,但当我尝试像指南中指定的那样使用它时,无法使其工作。我创建了一个在下载完成时调用的槽,但如果我尝试在槽内像指南中那样调用下载器,它会说未声明的标识符。
有谁知道如何正确实现这段代码吗?

这是我的代码的.cpp部分
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QStringList>
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <QList>
#include <QtNetwork/QNetworkReply>
#include <QStringList>
#include <QTimer>
#include <QUrl>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <filedownloader.h>
#include <iostream>
#include <QObject>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QUrl emiurl("您的文件的URL");
    // 调用下载器类。在指南中,它的制作方式不同,但不起作用
    FileDownloader emiload(emiurl, this);
    // 这个连接将下载结束与textwriter槽连接起来
    QObject::connect(&emiload, SIGNAL(downloaded()), this, SLOT(textwriter()));
}
MainWindow::~MainWindow()
{
    delete ui;
}
// 创建文本文件的槽,用于从下载的数据中创建txt文件
void MainWindow::textwriter()
{
    QByteArray emibyte;
    emibyte = emiload->downloadedData(); // 这一行给我错误
    QFile emifile("emi.txt");
    emifile.open(QIODevice::WriteOnly);
    std::cout << emibyte.size() << std::endl;
    QDataStream out(&emifile);
    out << emibyte;
    std::cout << emifile.size() << std::endl;
}

现在这是.h部分

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "filedownloader.h"
#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
private slots:
 void textwriter();
};
#endif // MAINWINDOW_H

请注意,上述代码中的文件路径和URL需要根据您的实际情况进行替换。

英文:

I am trying to download a txt file from a url in QT but i can't seem to make it work.
I am following this guide https://wiki.qt.io/Download_Data_from_URL. I implemented the filedownloader class exactly like it's made in the guide, but when i try to use it like specified in the guide I cannot make it work. I created a slot to be called when the download is finished, but if i try to call the downloader inside like the guide it says it is an undeclared identifier.
Does anyone know how to correctly implement this code?

this is the .cpp of my code

#include &quot;mainwindow.h&quot;
#include &quot;ui_mainwindow.h&quot;
#include &quot;mainwindow.h&quot;
#include &lt;QStringList&gt;
#include &lt;QCoreApplication&gt;
#include &lt;QFile&gt;
#include &lt;QFileInfo&gt;
#include &lt;QList&gt;
#include &lt;QtNetwork/QNetworkReply&gt;
#include &lt;QStringList&gt;
#include &lt;QTimer&gt;
#include &lt;QUrl&gt;
#include &lt;QtNetwork/QNetworkAccessManager&gt;
#include &lt;QtNetwork/QNetworkRequest&gt;
#include &lt;filedownloader.h&gt;
#include &lt;iostream&gt;
#include &lt;QObject&gt;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);
    QUrl emiurl( &quot;url of my file&quot;);
    // call to the downloader class.In the guide it&#39;s made differently, but it doesn&#39;t work
    FileDownloader emiload(emiurl,this); 
   //this connect links the end of the download with the textwriter slot
 QObject::connect(&amp;emiload, SIGNAL (downloaded()), this, SLOT (textwriter()));  

}
&gt;MainWindow::~MainWindow()
{
    delete ui;
}
//slot needed to create the txt file from the downloaded one
void MainWindow::textwriter() 
{
 QByteArray emibyte;
 emibyte=emiload-&gt;downloadedData(); //this line gives me error 
 QFile emifile(&quot;emi.txt&quot;);
 emifile.open(QIODevice::WriteOnly);
 std::cout &lt;&lt; emibyte.size() &lt;&lt; std::endl;
 QDataStream out(&amp;emifile);
 out &lt;&lt; emibyte;
 std::cout &lt;&lt; emifile.size() &lt;&lt; std::endl;
}

Now here's the .h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include &quot;filedownloader.h&quot;
#include &lt;QMainWindow&gt;
#include &lt;QtNetwork/QNetworkAccessManager&gt;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
private slots:
 void textwriter();
};
#endif // MAINWINDOW_H

答案1

得分: 1

为了让未声明的标识符消失并成功编译,您需要将FileDownloader添加到类的声明中。这样,它将在整个类中可见。

我选择了使用在Qt中通常的方法,声明一个指向FileDownloader的指针。

#pragma once // &lt;--- 这在今天几乎任何编译器中都受支持

#include &lt;QMainWindow&gt;
#include &lt;QtNetwork/QNetworkAccessManager&gt;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class FileDownloader; // &lt;-- 前向声明已足够,但您也可以#include &quot;filedownloader.h&quot;

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget* parent = nullptr);
    ~MainWindow();
private:
    Ui::MainWindow* ui = nullptr;
    FileDownloader* emiload = nullptr; // &lt;--- 重要的一行!
private slots:
    void textwriter();
};

然后在构造函数中实例化并调用emiload

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);
    // 使用"new"创建FileDownloader的实例。
    emiload = new FileDownloader(QUrl(&quot;文件的URL&quot;), this); 
    // 使用Qt5以来可用的成员指针连接
    connect(emiload, &amp;FileDownloader::downloaded, this, &amp;MainWindow::textwriter);  
}
英文:

To make the undeclared identifier go away and successfully compile, you need to add FileDownloader to the class' declaration. This way, it will be known throughout the class.

I chose to go with the approach that's usual in Qt, to declare a pointer to FileDownloader.

#pragma once // &lt;--- this is supported by virtually any compiler today

#include &lt;QMainWindow&gt;
#include &lt;QtNetwork/QNetworkAccessManager&gt;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class FileDownloader; // &lt;-- forward declaration is enough, but you can also #include &quot;filedownloader.h&quot;

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget* parent = nullptr);
    ~MainWindow();
private:
    Ui::MainWindow* ui = nullptr;
    FileDownloader* emiload = nullptr; // &lt;--- the important line!
private slots:
    void textwriter();
};

And then instantiate and call emiload in the constructor:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);
    // create an instance of FileDownloader with &quot;new&quot;.
    emiload = new FileDownloader(QUrl(&quot;url of my file&quot;), this); 
    // using member pointer connection available since Qt5
    connect(emiload, &amp;FileDownloader::downloaded, this, &amp;MainWindow::textwriter);  
}

huangapple
  • 本文由 发表于 2023年2月8日 21:38:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定