我有一个这样定义的QObject子类(在"Danbooru"命名空间内):
#ifndef DANBOORUPOST_H
#define DANBOORUPOST_H
// Qt
#include <QtCore/QObject>
#include <QtCore/QVariant>
#include <QtCore/QStringList>
#include <QtXml/QXmlStreamAttributes>
// KDE
#include <kurl.h>
class QPixmap;
namespace Danbooru {
class DanbooruPost : public QObject
{
Q_OBJECT
Q_PROPERTY(QPixmap* pixmap READ pixmap WRITE setPixmap)
Q_PROPERTY(int id READ id)
Q_PROPERTY(KUrl fileUrl READ fileUrl)
Q_PROPERTY(QStringList tags READ tags)
Q_PROPERTY(KUrl thumbnailUrl READ thumbnailUrl)
private:
QPixmap* m_pixmap;
// basic features of a post
int m_id;
int m_height;
int m_width;
int m_size;
KUrl m_url;
KUrl m_thumbnailUrl;
QStringList m_tags;
public:
explicit DanbooruPost(QVariantMap postData, QPixmap* pixmap = 0,
QObject* parent = 0);
explicit DanbooruPost(QXmlStreamAttributes& postData,
QPixmap* pixmap = 0, QObject* parent=0);
bool operator==(const DanbooruPost&);
~DanbooruPost();
// Post attributes
int id() const;
int height() const;
int width() const;
int size() const;
const KUrl fileUrl() const;
QStringList tags() const;
const KUrl thumbnailUrl() const;
QPixmap* pixmap() const;
void setPixmap(QPixmap* pixmap);
const QString toString();
};
}; // namespace Danbooru
Q_DECLARE_METATYPE(Danbooru::DanbooruPost*)
#endif // DANBOORUPOST_H
我在另一个定义如下的类中使用它:
#ifndef DANBOORUSERVICE_H
#define DANBOORUSERVICE_H
#include <QtCore/QObject>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <KUrl>
#include <KIO/JobClasses>
class QPixmap;
class KUrl;
class KJob;
class DanbooruPost;
namespace Danbooru {
enum TagType {
General,
Artist,
Copyright,
Character
};
using KIO::StoredTransferJob;
class DanbooruService : public QObject
{
Q_OBJECT
private:
// URL fragments
static const QString POST_URL;
static const QString TAG_URL;
static const QString POOL_URL;
static const QString ARTIST_URL;
static const QString POOL_DATA_URL;
static const QString RELATED_TAG_URL;
// member variables
KUrl m_url;
QString m_username;
QString m_password;
QStringList m_blacklist;
QString m_maxRating;
public:
DanbooruService(KUrl& boardUrl, QString username = QString(),
QString password = QString(),
QStringList blacklist = QStringList(),
QObject* parent=0);
~DanbooruService();
void getPostList(int page=1, QStringList tags=QStringList(),
int limit=100, QString rating="Safe");
void getPoolList(int page=0);
void getPool(int poolId, int page=1, QString rating="Safe");
void getTagList(int limit=10, QString name="");
void getRelatedTags(QStringList tags=QStringList(),
TagType tag_type=General);
QString allowedRating() const;
QStringList blacklist() const;
private Q_SLOTS:
void processPostList(StoredTransferJob* job);
void processPoolList(StoredTransferJob* job);
void processRelatedTagList(StoredTransferJob* job);
void downloadThumbnail(StoredTransferJob* job);
void downloadAllTags(StoredTransferJob* job);
Q_SIGNALS:
void postDownloadFinished();
void poolDownloadFinished();
void downloadError(QString error);
void postDownloaded(DanbooruPost*&post);
void postMetadataDownloaded(DanbooruPost* post);
// TODO: Tags and similar
};
};
我遇到的问题源于以下代码(StoredTransferJob是一个KDE类):
void DanbooruService::downloadThumbnail(StoredTransferJob* job)
{
if (job->error()) {
Q_EMIT(downloadError(job->errorString()));
}
QVariant postData = job->property("danbooruPost");
DanbooruPost* post = (DanbooruPost*) postData.data();
QPixmap* pix = new QPixmap();
bool ok = pix->loadFromData(job->data());
if (!ok) {
Q_EMIT(downloadError(QString("Pixmap data could not be loaded")));
}
post->setPixmap(pix);
Q_EMIT(postDownloaded(post));
}
然而,在编译时,Q_EMIT部分会生成以下错误:
/home/lb/Coding/cpp/danbooru_client/src/libdanbooru/danbooruservice.cpp: In member function ‘void Danbooru::DanbooruService::downloadThumbnail(KIO::StoredTransferJob*)’:
/home/lb/Coding/cpp/danbooru_client/src/libdanbooru/danbooruservice.cpp:186:35: error: no matching function for call to ‘Danbooru::DanbooruService::postDownloaded(Danbooru::DanbooruPost*&)’
/home/lb/Coding/cpp/danbooru_client/src/libdanbooru/danbooruservice.cpp:186:35: note: candidate is:
In file included from /home/lb/Coding/cpp/danbooru_client/src/libdanbooru/danbooruservice.cpp:30:0:
/home/lb/Coding/cpp/danbooru_client/src/libdanbooru/danbooruservice.h:104:14: note: void Danbooru::DanbooruService::postDownloaded(DanbooruPost*&)
/home/lb/Coding/cpp/danbooru_client/src/libdanbooru/danbooruservice.h:104:14: note: no known conversion for argument 1 from ‘Danbooru::DanbooruPost*’ to ‘DanbooruPost*&’
生成函数中使用的变体的代码是:
void DanbooruService::processPostList(KIO::StoredTransferJob* job)
{
qDebug() << "Got data OK";
if (job->error()) {
Q_EMIT(downloadError(job->errorString()));
}
QByteArray data = job->data();
QJson::Parser parser;
bool ok;
QVariant result = parser.parse(data, &ok);
if (!ok) {
Q_EMIT(downloadError(QString("Unable to decode data")));
}
QList<QVariant> postList = result.toList();
Q_FOREACH(QVariant element, postList) {
QVariantMap map = element.toMap();
DanbooruPost* post = new DanbooruPost(map);
StoredTransferJob* pixmapJob = KIO::storedGet(post->thumbnailUrl(),
KIO::NoReload, KIO::HideProgressInfo);
qDebug() << "Basic job info";
qDebug() << post->id() << post->thumbnailUrl() << post->fileUrl();
QVariant variant;
variant.setValue(post);
pixmapJob->setProperty("danbooruPost", variant);
connect(pixmapJob, SIGNAL(result(KJob*)), this,
SLOT(downloadThumbnail(StoredTransferJob*)));
}
我被这个错误弄糊涂了。我可能做错了什么?
您已经在全局命名空间中,在namespace Danbooru
之外,正向声明了class DanbooruPost
。您最终得到了两个不同的类,::DanbooruPost
和::Danbooru::DanbooruPost
。
如果您无意这样做,请从全局命名空间中删除正向声明。