自定义 QML QQuick彩绘项目成员锚点未在 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
的父级。 你想做anchors.fill: pdfDocument
.
或者,如果总是需要这样做,将其锚定在 c++ 中并避免在 QML 中执行此操作可能是有意义的。
相关文章:
- 自定义 QML QQuick彩绘项目成员锚点未在 qml 中设置为父级
- 是否可以在单独的线程中将 QObject 设置为 QML 上下文属性?
- 如何在QML中设置鼠标光标位置
- 如何将 QML 实例化项设置为 QImageProvider
- QML:设置动画图像源,而不加载资源中的文件
- 如何将C 类设置为QML中的项目
- QML filedialog设置标题来自C 代码
- 使用 QQuickWidget 设置 QML 主题
- 如何通过QT 5.6设置QML应用程序窗口透明
- C++到QML-设置属性
- 如何在QML无窗口应用程序中设置拖放区域
- 单击按钮后对QML矩形的颜色设置动画
- 从C++设置变量时 QML 内存泄漏
- 无法将 QVariantMap 设置为 QML 属性
- 使用c++设置qml对象的属性
- 为console.log设置一个QML类别
- 访问QML中使用setProperty设置的属性
- 在QT 5中重新设置QT/QML映射
- Qt/Qml:自定义小部件 -- 无法设置"width"属性,因为它是只读的
- 如何在Blackberry 10中设置从QML到CPP的属性变量