简体   繁体   中英

Custom QML QQuickPainted Item Member anchors not set to parent in qml

I am having a problem getting a member of a custom QQuickItem I created to anchor to its parent. I know that it is loading and the constructor is running because of some debug statements I placed, however for some reason anchoring to parent is not working on the sub-object.

Note: lots of shortening of code happened here. I hope everything is relevant without being overwhelming

qml Snippet

    PDFDocument
    {
        id: pdfDocument
        anchors.fill: parent

        visible: false
        pageView
        {
            dpi: 200

             //this is not working and paint is not being called
             //QML PDFPageView: Cannot anchor to an item that isn't a parent or sibling.
            anchors.fill: parent        
        }
    }

c++ code snippets

// PDFPageView.h
namespace TechnicalPublications
{

class PDFPageView : public QQuickPaintedItem
{
public:
    Q_OBJECT

    Q_PROPERTY( int dpi MEMBER m_dpi NOTIFY dpiChanged )

Q_SIGNALS:
    void dpiChanged();
public:
    PDFPageView( QQuickItem* parent = nullptr );
    ~PDFPageView();

    void setPage( Poppler::Page* page_p );

    void paint( QPainter* painter_p );

private:
    Poppler::Page* m_page_p;
};

}

//PDFPage.cpp
namespace TechnicalPublications
{

PDFPageView::PDFPageView( QQuickItem* parent )
    : QQuickPaintedItem( parent )
{
    LOG_DEBUG( __FILE__, __LINE__ ) << "Page parent" << parent;
    LOG_DEBUG( __FILE__, __LINE__ ) << "constructing PageView" << this;
}

void PDFPageView::setPage( Poppler::Page* page_p )
{
    m_page_p = page_p;
    update();
}

void PDFPageView::paint( QPainter* painter_p )
{
    LOG_DEBUG( __FILE__, __LINE__ ) << "painting pdf page";
    //deleted sections for spacing, point is paint is not called because size is 0
}

}

//PDFDocument.h
class PDFDocument : public QQuickItem
{
public:
    Q_OBJECT

    Q_PROPERTY( TechnicalPublications::PDFPageView* pageView READ getPageView )

    PDFDocument( QQuickItem* parent = nullptr );
    ~PDFDocument();

    PDFPageView* getPageView() { return &m_pageView; }
private:
    PDFPageView m_pageView;
};

}
#endif // PDFDOCUMENT_H

//PDFDocument.cpp
namespace TechnicalPublications
{

PDFDocument::PDFDocument( QQuickItem* parent /*= nullptr*/ )
    : QQuickItem( parent ),
      m_pageView( this )
{
    LOG_DEBUG( __FILE__, __LINE__ ) << "Document parent " << parent;
    LOG_DEBUG( __FILE__, __LINE__ ) << "constructing Document " << this;
}

PDFDocument::~PDFDocument()
{
}

}

I would even be happy to set the anchors to always take the parent in the c++ is possible, but I know visual settings like that are supposed to be handled in QML specifically. Any thoughts on why this is being a problem?

It's because the scope of your grouped property (just a nested QObject* property), is the same of the parent object.

so when you do:

PDFDocument {
    id: pdfDocument
    pageView {
        anchors.fill: parent        
    }
}

parent is referring to the parent of pdfDocument . You want to do anchors.fill: pdfDocument .

Alternatively it might makes sense to anchor it in c++ and avoid doing it in QML if always need to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM