Fetch text from a site as QString
-
I see thanks.
If I declare it asQNetworkAccessManager manager;
I have errors so I made it like this:private: QNetworkAccessManager* m_manager = new QNetworkAccessManager(this); void MyClass::downloadFile() { connect(m_manager, &QNetworkAccessManager::finished, this, &MyClass::replyFinished); manager->get(QNetworkRequest(QUrl( "https://...file"))); } void MyClass::replyFinished(QNetworkReply *reply) { if (reply->error() == QNetworkReply::NoError) { QByteArray data = reply->readAll(); QFile file(<file>); if (file.open(QIODevice::WriteOnly)) { QTextStream out(&file); out << data; file.close(); doSomething(<file>); } } reply->deleteLater(); }
-
@realroot said in Fetch text from a site as QString:
I see thanks.
If I declare it asQNetworkAccessManager manager;
I have errors so I made it like this:private: QNetworkAccessManager* m_manager = new QNetworkAccessManager(this); void MyClass::downloadFile() { connect(m_manager, &QNetworkAccessManager::finished, this, &MyClass::replyFinished);
Move that connect to the constructor of your class. Otherwise each time you call
downloadFile
you will create a new connection which means that the slot will be called an additional time. -
Then it should be so I believe:
class MyClass : public QAbstractListModel { Q_OBJECT public: MyClass(QObject *parent = nullptr) : QAbstractListModel(parent) { connect(m_manager, &QNetworkAccessManager::finished, this, &MyClass::downloadFinished); } private: QNetworkAccessManager* m_manager = new QNetworkAccessManager(this); void MyClass::downloadFile() { m_manager->get(QNetworkRequest(QUrl( "https://...file"))); }
It's working at least.
-
@realroot
It looks reasonable. Although yourQNetworkAccessManager* m_manager = new QNetworkAccessManager(this);
will work personally I would do the
new
in theMyClass
constructor, to the line above where you have moved theconnect()
like @SGaist said. But maybe that's just me. In any case I believe your code is now acceptable. -
-
C++ now allows to initialize class variable at declaration spot. It's easy and nice for base types however, it can make things harder to read for complex type.
Next, unless you have complex logic associated, you should use your class initializer list and after that the constructor. This will help the compiler optimize some things.
-
@Pl45m4 Functions (
downloadFile
andreplyFinished
) are not in the header. I did not specify that but I did add MyClass scope e.g.void MyClass::downloadFile()
.@SGaist So:
MyClass(QObject *parent = nullptr) : QAbstractListModel(parent), m_manager(new QNetworkAccessManager(this)) { connect(m_manager, &QNetworkAccessManager::finished, this, &MyClass::downloadFinished); }
-
@realroot said in Fetch text from a site as QString:
@Pl45m4 Functions ( downloadFile and replyFinished) are not in the header. I did not specify that but I did add MyClass scope e.g. void MyClass::downloadFile().
Don't know what this means, but might be okay.