0

Here is a sample qt code that is copied from qt documentation site.

#include <QtCore/QCoreApplication>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QList>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QSslError>
#include <QtCore/QStringList>
#include <QtCore/QTimer>
#include <QtCore/QUrl>
#include <QtCore>

#include <stdio.h>

class QSslError;

QT_USE_NAMESPACE

class DownloadManager: public QObject
{
    Q_OBJECT

    QNetworkAccessManager manager;
    QList<QNetworkReply *> currentDownloads;

public:
    DownloadManager();
    void doDownload(const QUrl & url);
    QString saveFileName(const QUrl & url);
    bool saveToDisk(const QString & fileName, QIODevice *data);
    virtual ~DownloadManager();

public slots:
    void execute();
    void downloadFinished(QNetworkReply *reply);
    void sslErrors(const QList<QSslError> & errors);

};


DownloadManager::~DownloadManager()
{

}

DownloadManager::DownloadManager()
{
    connect(&manager, SIGNAL(finished(QNetworkReply*)), SLOT(downloadFinished(QNetworkReply*)));
}

void DownloadManager::doDownload(const QUrl & url)
{
    QNetworkRequest request(url);
    QNetworkReply* reply = manager.get(request);
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)), SLOT(sslErrors(QList<QSslError>)));
    currentDownloads.append(reply);
}


QString DownloadManager::saveFileName(const QUrl & url)
{
    QString path = url.path();
    QString basename = QFileInfo(path).fileName();

    if ( basename.isEmpty())
        basename = "download";

    if ( QFile::exists(basename))
    {
        int i=0;
        basename += '.';
        while ( QFile::exists(basename + QString::number(i)))
            i++;
        basename += QString::number(i);
    }

    return basename;

}

bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data)
{
    QFile file(filename);
    if ( !file.open(QIODevice::WriteOnly))
    {
        fprintf(stderr, "Could not open %s for writing: %s\n",
                qPrintable(filename),
                qPrintable(file.errorString()));
        return false;
    }

    file.write(data->readAll());
    file.close();

    return true;
}


void DownloadManager::execute()
{
    QStringList args = QCoreApplication::instance()->arguments();
    args.takeFirst();

    if ( args.empty() )
    {
        printf("blah blah");
        QCoreApplication::instance()->quit();
        return;
    }

    foreach(QString arg, args)
    {
        QUrl url = QUrl::fromEncoded(arg.toLocal8Bit());
        doDownload(url);
    }
}


void DownloadManager::sslErrors(const QList<QSslError> &errors)
{
//#ifndef QT_NO_OPENSSL
    foreach(const QSslError & error, errors)
        fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString()));
//#endif
}


void DownloadManager::downloadFinished(QNetworkReply *reply)
{
    QUrl url = reply->url();
    if ( reply->error() )
    {
        fprintf(stderr, "Download of %s failed: %s\n",
                url.toEncoded().constData(),
                qPrintable(reply->errorString()));
    }
    else
    {
        QString filename = saveFileName(url);
        if ( saveToDisk(filename, reply))
            printf("Download of %s succeeded 9saved to %s\n",
                   url.toEncoded().constData(), qPrintable(filename));
    }

    currentDownloads.removeAll(reply);
    reply->deleteLater();

    if ( currentDownloads.isEmpty())
        QCoreApplication::instance()->quit();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    DownloadManager manager;
    //QTimer::singleShot(0, &manager, SLOT(execute()));

    return a.exec();
}

the .pro file is

#-------------------------------------------------
#
# Project created by QtCreator 2013-04-17T11:17:07
#
#-------------------------------------------------

QT       += core network

QT       -= gui

TARGET = network3
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

As this is directly copied from the authentic site, it should build properly. But its not building.

The error this is showing..

asit@ubuntu:~/qt/network3-build-desktop$ make
g++ -Wl,-O1 -o network3 main.o    -L/usr/lib/i386-linux-gnu -lQtNetwork -lQtCore -lpthread 
main.o: In function `DownloadManager::DownloadManager()':
main.cpp:(.text+0x584): undefined reference to `vtable for DownloadManager'
main.o: In function `DownloadManager::~DownloadManager()':
main.cpp:(.text+0x61a): undefined reference to `vtable for DownloadManager'
collect2: ld returned 1 exit status
make: *** [network3] Error 1

I followed most of the steps in the internet(which includes stackoverflow) to get rid of the error. But it does not work.

Can somebody show me some light in this darkness ?

4

2 回答 2

3

将您的类接口放在 .h 文件中,问题应该会消失!

于 2013-04-17T19:03:43.410 回答
3

您有 2 个选项。

#1

将此类保留在main.cpp文件中,但在其底部添加以下内容:

#include "main.moc"

#2

将类的定义移动到头文件中,例如main.h,不要忘记在文件中添加以下内容*.pro

HEADERS += main.h

注意:您不应将整个类放入main.h中,而应仅将定义放入:

class DownloadManager: public QObject
{
    Q_OBJECT
    ...
};

并且执行可以留在main.cpp.

于 2013-04-17T19:12:48.350 回答