Adding code example to QT C++ -
i'd add qt example code simple project. sample code here: https://wiki.qt.io/download_data_from_url
it consists of filedownloader.cpp , filedownloader.h -- code downloads graphic supplied url.
i've added these files project , clean compile. think understand code ok (i'm c coder, not c++) don't understand how can pass qurl created project filedownloader.cpp
the "project" simple main.cpp/mainwindow.cpp/mainwindow.ui offers button pressed. pressing button calls routine below:
void mainwindow::on_pushbutton_clicked() { // pass filedownloader process qurl fileloc("http://www.test.com/test.jpg"); }
how feed qurl fileloc filedownload.cpp?
you have add new method filedownloader, accepts qurl , starts download.
filedownloader.h:
#ifndef filedownloader_h #define filedownloader_h #include <qobject> #include <qbytearray> #include <qnetworkaccessmanager> #include <qnetworkrequest> #include <qnetworkreply> class filedownloader : public qobject { q_object public: explicit filedownloader(qurl imageurl, qobject *parent = 0); virtual ~filedownloader(); qbytearray downloadeddata() const; signals: void downloaded(); public slots: void download(qurl url); // <------ here private slots: void filedownloaded(qnetworkreply* preply); private: qnetworkaccessmanager m_webctrl; qbytearray m_downloadeddata; }; #endif // filedownloader_h
filedownloader.cpp:
#include "filedownloader.h" filedownloader::filedownloader(qobject *parent) : qobject(parent) { connect( &m_webctrl, signal (finished(qnetworkreply*)), this, slot (filedownloaded(qnetworkreply*)) ); // <------ notice, i've removed downloading code here } filedownloader::~filedownloader() { } void filedownloader::filedownloaded(qnetworkreply* preply) { m_downloadeddata = preply->readall(); //emit signal preply->deletelater(); emit downloaded(); } void filedownloader::download(qurl url) { // <------ , definition qnetworkrequest request(url); m_webctrl.get(request); } qbytearray filedownloader::downloadeddata() const { return m_downloadeddata; }
and on_pushbutton_clicked
this:
void mainwindow::on_pushbutton_clicked() { // pass filedownloader process qurl fileloc("http://www.test.com/test.jpg"); m_filedownloader.download(fileloc); }
Comments
Post a Comment