gpt4 book ai didi

c++ - 与 Q_OBJECT 相关的 qt 链接错误

转载 作者:太空宇宙 更新时间:2023-11-04 15:46:19 26 4
gpt4 key购买 nike

这是从 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

我按照互联网上的大部分步骤(包括 stackoverflow)来消除错误。但它不起作用。

谁能在这黑暗中给我点亮光?

最佳答案

将类接口(interface)放在 .h 文件中,问题就会消失!

关于c++ - 与 Q_OBJECT 相关的 qt 链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16067734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com