繁体   English   中英

自定义 QML QQuickPainted Item Member 锚点未设置为 qml 中的父级

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

我在获取我创建的自定义 QQuickItem 的成员以锚定到其父项时遇到问题。 我知道它正在加载并且构造函数正在运行,因为我放置了一些调试语句,但是由于某种原因锚定到父对象不能在子对象上工作。

注意:这里发生了很多代码的缩短。 我希望一切都是相关的,而不是压倒性的

qml 片段

    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++ 代码片段

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

}

我什至很乐意将锚点设置为始终采用 c++ 中的父项是可能的,但我知道应该专门在 QML 中处理这样的视觉设置。 关于为什么这是一个问题的任何想法?

这是因为您的分组属性(只是一个嵌套的 QObject* 属性)的范围与父对象相同。

所以当你这样做时:

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

parent指的是pdfDocument的父pdfDocument 你想做anchors.fill: pdfDocument

或者,将它锚定在 C++ 中并避免在 QML 中执行它(如果总是需要这样做)可能是有意义的。

暂无
暂无

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

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