c++错误:使用Qt和QVariant,没有从指针到指针引用的匹配函数调用。

时间:2022-11-30 09:44:07

I have a QObject subclass defined as such (inside a "Danbooru" namespace):

我定义了一个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

and I'm using it in another class defined like this:

我在另一个类中使用它定义如下:

#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

    };
};

The problem I'm experiencing arises from this code (StoredTransferJob is a KDE class):

我正在经历的问题源自这个代码(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));

    }

However when compiled, the Q_EMIT part generates this error:

然而,当编译时,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*&’

The code that generates the variant used in the function is:

生成用于函数的变量的代码是:

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*)));

    }

I'm absolutely baffled by this error. What might I be doing wrong?

我对这个错误感到非常困惑。我可能做错了什么?

1 个解决方案

#1


1  

You have forward-declared class DanbooruPost in the global namespace, outside of namespace Danbooru. You have ended up with two distinct clases, ::DanbooruPost and ::Danbooru::DanbooruPost.

您在名称空间Danbooru之外的全局名称空间中有了前瞻性的类DanbooruPost。你最终得到了两个截然不同的答案:DanbooruPost和:Danbooru::DanbooruPost。

If this is not your intention, remove the forward declaration from the global namespace.

如果这不是您的意图,请从全局名称空间中删除forward声明。

#1


1  

You have forward-declared class DanbooruPost in the global namespace, outside of namespace Danbooru. You have ended up with two distinct clases, ::DanbooruPost and ::Danbooru::DanbooruPost.

您在名称空间Danbooru之外的全局名称空间中有了前瞻性的类DanbooruPost。你最终得到了两个截然不同的答案:DanbooruPost和:Danbooru::DanbooruPost。

If this is not your intention, remove the forward declaration from the global namespace.

如果这不是您的意图,请从全局名称空间中删除forward声明。