Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Code copied from 'http' example has issues



  • When I copied the code from the example into my own project I found that
    #include <QtNetwork> has to be #include <QtNetwork/QtNetwork> and #include <QNetworkAccessManager> has to be #include <QtNetwork/QNetworkAccessManager> the 'connect' method in the http events wants to be derived from a class and I get an error in a file that I will not be ready to understand for ten years some 'meta object thing'

    the header is

    #ifndef GETHTML_H
    #define GETHTML_H
    #include <QProgressDialog>
    #include <QtNetwork/QNetworkAccessManager>
    #include <QUrl>
    #include <memory>
    class gethtml
    {
    public:
        gethtml();
        QString htmlContent;
        QString errorText;
        QString user;
        QString password;
        bool haslod;
        void downloadFile(QString myurl);
        //QVector<QChar> binContent;
    private slots:
        void startRequest(const QUrl &requestedUrl);
        void httpFinished();
        void httpReadyRead();
        void sslErrors(const QList<QSslError> &errors);
        void slotAuthenticationRequired(QNetworkReply *, QAuthenticator *authenticator);
    private:
        QUrl url;
        QNetworkAccessManager qnam;
        QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply;
        bool httpRequestAborted = false;
    };
    
    #endif // GETHTML_H
    
    

    the source is

    
    #include "gethtml.h"
    
    
    
    
    #include <QtNetwork/QtNetwork>
    #include <QUrl>
    
    #include <algorithm>
    #include <memory>
    
    //#if QT_CONFIG(ssl)
    gethtml::gethtml(){
    QNetworkAccessManager::connect(&qnam, &QNetworkAccessManager::authenticationRequired,
            this, &gethtml::slotAuthenticationRequired);
    }
    void gethtml::startRequest(const QUrl &requestedUrl)
    {
        url = requestedUrl;
        httpRequestAborted = false;
    
        //! [qnam-download]
        reply.reset(qnam.get(QNetworkRequest(url)));
        //! [qnam-download]
        //! [connecting-reply-to-slots]
        connect(reply.get(), &QNetworkReply::finished, this, &gethtml::httpFinished);
        //! [networkreply-readyread-1]
        connect(reply.get(), &QIODevice::readyRead, this, &gethtml::httpReadyRead);
        //! [networkreply-readyread-1]
    #if QT_CONFIG(ssl)
        //! [sslerrors-1]
        connect(reply.get(), &QNetworkReply::sslErrors, this, &gethtml::sslErrors);
        //! [sslerrors-1]
    #endif
        //! [connecting-reply-to-slots]
    
    errorText = "";
    haslod = false;
    
    
    
    
    
    //    statusLabel->setText(tr("Downloading %1...").arg(url.toString()));
    }
    
    void gethtml::downloadFile(QString myurl)
    {
        const QString urlSpec = myurl.trimmed();
        if (urlSpec.isEmpty())
            return;
    
        const QUrl newUrl = QUrl::fromUserInput(urlSpec);
        if (!newUrl.isValid()) {
    
            return;
        }
    
    
    
    
    
        // schedule the request
        startRequest(newUrl);
    }
    
    
    
    
    
    void gethtml::httpFinished()
    {
    
    
        //! [networkreply-error-handling-1]
        QNetworkReply::NetworkError error = reply->error();
        const QString &errorString = reply->errorString();
        //! [networkreply-error-handling-1]
        reply.reset();
        //! [networkreply-error-handling-2]
        if (error != QNetworkReply::NoError) {
    
            // For "request aborted" we handle the label and button in cancelDownload()
            if (!httpRequestAborted) {
                errorText = reply->error();
    
            }
            return;
        }
        //! [networkreply-error-handling-2]
    
        //statusLabel->setText(tr("Downloaded %1 bytes to %2\nin\n%3"));
    
       // if (launchCheckBox->isChecked())
        //    QDesktopServices::openUrl(QUrl::fromLocalFile(fi.absoluteFilePath()));
        //downloadButton->setEnabled(true);
        haslod = true;
    }
    
    //! [networkreply-readyread-2]
    void gethtml::httpReadyRead()
    {
        // This slot gets called every time the QNetworkReply has new data.
        // We read all of its new data and write it into the file.
        // That way we use less RAM than when reading it at the finished()
        // signal of the QNetworkReply
    
            htmlContent += reply->readAll();
    
    }
    //! [networkreply-readyread-2]
    
    
    
    //! [qnam-auth-required-2]
    void gethtml::slotAuthenticationRequired(QNetworkReply *, QAuthenticator *authenticator)
    {
       // QDialog authenticationDialog;
        //Ui::Dialog ui;
        //ui.setupUi(&authenticationDialog);
        //authenticationDialog.adjustSize();
        //ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm(), url.host()));
    
        // Did the URL have information? Fill the UI.
        // This is only relevant if the URL-supplied credentials were wrong
        //ui.userEdit->setText(url.userName());
        //ui.passwordEdit->setText(url.password());
    
        //if (authenticationDialog.exec() == QDialog::Accepted) {
            authenticator->setUser(user);
            authenticator->setPassword(password);
      //  }
    }
    //! [qnam-auth-required-2]
    
    //#if QT_CONFIG(ssl)
    //! [sslerrors-2]
    void gethtml::sslErrors(const QList<QSslError> &errors)
    {
        QString errorString = "";
        for (const QSslError &error : errors) {
            if (!errorString.isEmpty())
                errorString += '\n';
            errorString += error.errorString();
        }
        errorText = errorString;
    
            //reply->ignoreSslErrors();
    
    }
    //! [sslerrors-2]
    //#endif
    
    

    I hope someone can help me.


  • Lifetime Qt Champion

    @AI_Messiah said in Code copied from 'http' example has issues:

    When I copied the code from the example into my own project I found that

    You forgot to add the QtNetwork module as described in the documentation and the example also for sure has this line in the .pro-file

    qmake: QT += network



  • Actually I did insert this 'QT += network' in the project file


  • Lifetime Qt Champion

    @AI_Messiah Then the compiler and linker will also use the correct include paths and libs. Don't forget to rerun qmake and you modified the correct pro file.



  • @AI_Messiah said in Code copied from 'http' example has issues:

    and I get an error in a file that I will not be ready to understand for ten years some 'meta object thing'

    After you have followed @Christian-Ehrlicher's instructions to rebuild thoroughly, if you still get this error you should paste it rather than giving us your interpretation! :)


Log in to reply