Important: Please read the Qt Code of Conduct -

call to non-static member function without an object argument

  • I want to make file downloader function that can be called in any place in the programm

    But I'm getting that error: call to non-static member function without an object argument

    What's wrong


    #include <QtCore>
    #include <QtNetwork>
    #include <cstdio>
    #ifndef DOWNLOAD_H
    #define DOWNLOAD_H
    class QSslError;
    using namespace std;
    class download: public QObject
        QNetworkAccessManager manager;
        QVector<QNetworkReply *> currentDownloads;
            QString dir;
            void doDownload(QUrl url, QString directoryName);
            static QString saveFileName(const QUrl &url);
            bool saveToDisk(const QString &filename, QIODevice *data);
            static bool isHttpRedirect(QNetworkReply *reply);
        public slots:
            void execute();
            void downloadFinished(QNetworkReply *reply);
            void sslErrors(const QList<QSslError> &errors);
    #endif // DOWNLOAD_H


    #include "download.h"
    download manager();
        connect(&manager, SIGNAL(finished(QNetworkReply*)),
        QTimer::singleShot(0, &manager, SLOT(execute()));
    void download::doDownload(QUrl url, QString directoryName)
        dir = directoryName;
        QNetworkRequest request(url);
        QNetworkReply *reply = manager.get(request);
    #if QT_CONFIG(ssl)
        connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
    QString download::saveFileName(const QUrl &url)
        QString path = url.path();
        QString basename = QFileInfo(path).fileName();
        if (basename.isEmpty())
            basename = "download";
        if (QFile::exists(basename)) {
            // already exists, don't overwrite
            int i = 0;
            basename += '.';
            while (QFile::exists(basename + QString::number(i)))
            basename += QString::number(i);
        return basename;
    bool download::saveToDisk(const QString &filename, QIODevice *data)
        QString filePath = dir + '/' + filename;
        QFile file(filePath);
        if (! {
            fprintf(stderr, "Could not open %s for writing: %s\n",
            return false;
        return true;
    bool download::isHttpRedirect(QNetworkReply *reply)
        int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        return statusCode == 301 || statusCode == 302 || statusCode == 303
               || statusCode == 305 || statusCode == 307 || statusCode == 308;
    void download::execute()
        QUrl url = QUrl::fromEncoded(QString("").toLocal8Bit());
    void download::sslErrors(const QList<QSslError> &sslErrors)
    #if QT_CONFIG(ssl)
        for (const QSslError &error : sslErrors)
            fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString()));
    void download::downloadFinished(QNetworkReply *reply)
        QUrl url = reply->url();
        if (reply->error()) {
            fprintf(stderr, "Download of %s failed: %s\n",
        } else {
            if (isHttpRedirect(reply)) {
                fputs("Request was redirected.\n", stderr);
            } else {
                QString filename = saveFileName(url);
                if (saveToDisk(filename, reply)) {
                    printf("Download of %s succeeded (saved to %s)\n",
                           url.toEncoded().constData(), qPrintable(filename));
        if (currentDownloads.isEmpty()) {
            // all downloads finished
    #include "main.moc"


    #include <QtCore>
    #include <QtNetwork>
    #include <cstdio>
    #include <download.h>
    int main(int argc, char **argv)
        QUrl bruh = QUrl::fromEncoded(QString("").toLocal8Bit());
        download::doDownload(bruh, QString(""));
    #include "main.moc"

    problem with that line of code in main.cpp: download::doDownload(bruh, QString("TEST"));

    Help please.

  • Man, you should start with the C++ basics.

  • @InferusAnima
    Either you need to create an instance of download, or you need to rewrite a static version.

  • @Bonnie Yes I know, i'm bad at C++, but I rly need it, mb you have solution how to fix?

  • @InferusAnima
    Even this error is fixed, this application still cannot work.
    It has no event loop, the asynchronous network operations won't survive.
    Since you are using Qt classes, when starting a new project, you should choose Qt Widgets/Console Application templates.
    They'll give you a QCoreApplication/QApplication to start the event loop.

  • @Bonnie yeah, ik, that just test thing... i just need to fix that error. I just need ability to call download function everywhere in actual programm.

  • @InferusAnima
    Well, that should be easy.
    First, add an extern declaration on the bottom of download.h:

    extern download manager;

    Second on the top of download.cpp

    //change to this if get error on this line: download manager()
    download manager;

    Last in the main.cpp

    manager.doDownload(bruh, QString(""));

    You may also need to delete this bottom line in the .cpps

    #include "main.moc"

  • @Bonnie It worked, thank you very much!

  • This post is deleted!

  • Lifetime Qt Champion

    @vatsk Please read and follow else you will be baned!
    I will delete your post as inappropriate and offensive.

Log in to reply