Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

Problems with a downloader-class

$
0
0
Hello! We want to extract some information from the web by creating a class “downloader”. The problem is that the download works if we calls the member function from main.cpp, but it doesn’t work if we call it elsewhere. We are completely out of ideas at the moment and would really appreciate som help! The files are included below. The hedaer file downloader.h #ifndef DOWNLOADER_H #define DOWNLOADER_H #include <QObject> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkRequest> #include <QtNetwork/QNetworkReply> #include <QUrl> #include <QDateTime> #include <QFile> #include <QDebug>   class Downloader : public QObject {     Q_OBJECT public:     explicit Downloader(QObject *parent = 0);     void doDownload(QString);   signals:   public slots:     void replyFinished (QNetworkReply *reply);   private:    QNetworkAccessManager *manager; }; #endif // DOWNLOADER_H downloader.cpp #include "downloader.h" #include <QString> #include <QDebug> #include <iostream> #include <string>   Downloader::Downloader(QObject *parent) :     QObject(parent) { }   void Downloader::doDownload(QString input) {     QString *url = new QString("http://thetvdb.com/api/GetSeries.php?seriesname=");     url->append(input);       manager = new QNetworkAccessManager(this);     manager->get(QNetworkRequest(QUrl(QUrl::fromUserInput(*url))));       connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*))); }   void Downloader::replyFinished(QNetworkReply *reply) {     if(reply->error())     {         qDebug() << "ERROR!";         qDebug() << reply->errorString();     }     else     {         qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();         qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();         qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();         qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();         qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();           QFile *file = new QFile("C:\\Scallion\\STORAGE\\serie.txt");         if(file->open(QFile::Append))         {             file->write(reply->readAll());             file->flush();             file->close();         }         delete file;     }     reply->deleteLater(); } main.cpp. In this content the download works #include "mainwindow.h" #include "downloader.h" #include <QApplication> #include <QString>   int main(int argc, char *argv[]) {     QApplication a(argc, argv);       MainWindow w;     w.setWindowTitle("Scallion");     w.show();         Downloader d;     QString *str = new QString("modern+family");     qDebug() << str;     d.doDownload(*str);       return a.exec(); } but it doesn’t work if you put the line of codes in this content in another file, mainwindow.cpp… (the whole file isn’t added, only where the call is done) void MainWindow::on_searchFrameButton_clicked() {     addShowActions(); }   void MainWindow::on_tvshowName_returnPressed() {     addShowActions(); } void MainWindow::addShowActions() {     ui->viewLabel2->hide();     std::string tvshowname = ui->tvshowName->text().toStdString();     //qDebug() << ui->tvshowName->text();     SEARCH searchObject = tvshowname;     bool added = searchObject.get_data();     ui->tvshowName->clear();     //privateLayout();       if (added)     {           Downloader d;         QString *str = new QString("modern+family");         d.doDownload(*str);           ui->viewLabel2->setText(QString::fromStdString(tvshowname) + " was succesfully added");         ui->viewLabel2->show();     }     else     {         ui->viewLabel2->setText(QString::fromStdString(tvshowname) + " was succesfully NOT added");         ui->viewLabel2->show();     } } Thanks alot!

Viewing all articles
Browse latest Browse all 13965

Trending Articles