[英]qt link error related to Q_OBJECT
这是从qt文档站点复制的样本qt代码。
#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();
}
.pro文件是
#-------------------------------------------------
#
# 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
由于这是直接从真实站点复制而来,因此应该正确构建。 但是它没有建立。
显示的错误。
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
我遵循了Internet(包括stackoverflow)中的大多数步骤来消除该错误。 但这行不通。
有人可以在这黑暗中给我一些光吗?
将您的类接口放在.h文件中,问题应该消失了!
您有2个选择。
将该类保留在main.cpp
文件中,但是在它的底部 ,您应该添加以下内容:
#include "main.moc"
将类的定义移动到头文件main.h
,不要忘记将以下内容添加到*.pro
文件:
HEADERS += main.h
注意:您不应将整个类放入main.h
,而main.h
其定义如下:
class DownloadManager: public QObject
{
Q_OBJECT
...
};
实现可以保留在main.cpp
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.