简体   繁体   English

扩展 QObject 构造函数的类“在此上下文中是私有的”

[英]Class extending QObject constructor "is private within this context"

I am trying to make a class extending QObject that contains a list of another class extending QObject.我正在尝试创建一个扩展 QObject 的类,其中包含一个扩展 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.我的目标是在 QML 中使用它来填充 ListView,但我似乎无法解决这个问题。

Any thoughts?有什么想法吗?

The problem is where you're putting Q_OBJECT .问题是你把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.请注意,它具有public:private:访问说明符,Q_OBJECT 之后的任何内容都将是私有的。 So you need to put it before your public:所以你需要把它放在你的public:面前public:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单例类构造函数的问题在此上下文中是私有的 - Issue with singleton class constructor is private within this context 私人班级成员-在这种情况下 - Private members of class - within this context &#39;QObject :: QObject&#39;无法访问类&#39;QObject&#39;中声明的私有成员 - 'QObject::QObject' cannot access private member declared in class 'QObject' `QObject`子类和复制构造函数出错:`QObject :: QObject(const QObject&)是private` - Error with `QObject` subclass and copy constructor: `QObject::QObject(const QObject&) is private` 错误:'class::class()' 在此上下文中是私有的 - error: ‘class::class()’ is private within this context 构造函数是使用Singleton C ++的私有错误,在此情况下错误 - constructor is private error using singleton c++ and within this context error 从基于QObject的类继承构造函数 - Inheriting constructor from QObject based class 错误:'VD<class1> Class2::data' 在此上下文中是私有的</class1> - error: 'VD<Class1> Class2::data' is private within this context 将值从构造函数传递到类中的私有整数 - Passing value from constructor to private integer within class 扩展具有私有构造函数和析构函数的单例类会给出编译时间警告 - Extending a singleton class which has private constructor and destructor gives compile time warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM