Class 扩展 QObject 构造函数 "is private within this context"

Class extending QObject constructor "is private within this context"

我正在尝试创建一个 class 扩展 QObject,其中包含另一个 class 扩展 QObject 的列表。在构造列表对象时,我将一个对象传递给构造函数,其中包含构造对象所需的所有点。但是,我的构造函数以某种方式被标记为私有时遇到问题。

//PDFOutline.h
public:
    Q_OBJECT

    Q_PROPERTY( QString name READ name CONSTANT )

    PDFOutlineItem( Poppler::OutlineItem outline, QObject* parent = nullptr );
    PDFOutlineItem( QObject* parent = nullptr);

    QString name() { return m_text; }

private:
    //QList<PDFOutlineItem> m_children;
    int m_page;
    QString m_text;
};

class PDFOutline : public QObject
{
public:
    Q_OBJECT

    PDFOutline( QVector<Poppler::OutlineItem> outline, QObject* parent = nullptr );
    PDFOutline( QObject* parent = nullptr );

    //QList<PDFOutlineItem> getOutline() { return m_outline; }

private:
    QList<PDFOutlineItem*> m_outline;
};
//PDFOutline.cpp
PDFOutline::PDFOutline( QVector<Poppler::OutlineItem> outline, QObject* parent )
    : QObject( parent )
{
    for( auto item : outline )
    {
        m_outline.append( new PDFOutlineItem( item, this ) );  //This is the line throwing the error
    }
}

PDFOutline::PDFOutline( QObject* parent )
    : QObject( parent )
{
}

PDFOutlineItem::PDFOutlineItem( Poppler::OutlineItem outlineItem, QObject* parent )
    : QObject( parent ),
      m_page( outlineItem.destination()->pageNumber() ),
      m_text( outlineItem.name() )
{
}

PDFOutlineItem::PDFOutlineItem( QObject* parent )
    : QObject( parent )
{
}

输出错误如下:

In file included from TechnicalPublications/PDFOutline.cpp:1:0:
TechnicalPublications/PDFOutline.h: In constructor 'TechnicalPublications::PDFOutline::PDFOutline(QVector<Poppler::OutlineItem>, QObject*)':
TechnicalPublications/PDFOutline.h:17:2: error: 'TechnicalPublications::PDFOutlineItem::PDFOutlineItem(Poppler::OutlineItem, QObject*)' is private
  PDFOutlineItem( Poppler::OutlineItem outline, QObject* parent = nullptr );
  ^
TechnicalPublications/PDFOutline.cpp:13:52: error: within this context
   m_outline.append( new PDFOutlineItem( item, this ) );
                                                    ^

希望我遗漏了一些愚蠢的东西,但这有可能吗?我的目标是在 QML 中使用它来填充 ListView,但我似乎无法解决这个问题。

有什么想法吗?

问题是你放在哪里 Q_OBJECT。这是我的定义:

/* qmake ignore Q_OBJECT */
#define Q_OBJECT \
public: \
    QT_WARNING_PUSH \
    Q_OBJECT_NO_OVERRIDE_WARNING \
    static const QMetaObject staticMetaObject; \
    virtual const QMetaObject *metaObject() const; \
    virtual void *qt_metacast(const char *); \
    virtual int qt_metacall(QMetaObject::Call, int, void **); \
    QT_TR_FUNCTIONS \
private: \
    Q_OBJECT_NO_ATTRIBUTES_WARNING \
    Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
    QT_WARNING_POP \
    struct QPrivateSignal {}; \
    QT_ANNOTATE_CLASS(qt_qobject, "")

请注意,它有 public:private: 访问说明符,Q_OBJECT 之后的任何内容都是私有的。所以你需要把它放在 之前 你的 public: