3
votes

I am trying to make a class extending QObject that contains a list of another class extending QObject. On construction of the list object, I am passing an object into the constructor containing all the points I need to construct my objects. However, I am having a problem with my constructors somehow being marked as private.

//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 )
{
}

The output error is the following:

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

Hopefully I am missing something silly, but is this even possible? My goal is to use this in QML to populate a ListView, but i cant seem to get past this isssue.

Any thoughts?

1
Perhaps the Q_OBJECT or Q_PROPERTY macros change access specifier to private? - Some programmer dude

1 Answers

6
votes

The problem is where you're putting Q_OBJECT. Here's the definition I have:

/* 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, "")

Note that it has public: and private: access specifiers, anything after a Q_OBJECT will be private. So you need to put it before your public: