【发布时间】:2020-01-10 22:26:44
【问题描述】:
我在获取我创建的自定义 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++代码sn-ps
// 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 中专门处理。关于为什么这是一个问题的任何想法?
【问题讨论】:
-
不应该是
pageView: PageView {}或类似的吗?我的意思是PDFPageView的实例在哪里创建?