Ownership with Q3DSurface
-
Hi everybody,
i use the Q3DSurface class, but i'm not quite sure if i need to destroy the instances in the destructor or not.MyClass.h
class MyClass : public QMainWindow { Q_OBJECT public: MyClass(QWidget*parent = 0); ~MyClass(); private: Ui::MyClass_GUIClass ui; Q3DSurface *m_graph; QWidget *m_grapWidget; MyAbstractListModel *m_model; QItemModelSurfaceDataProxy *m_proxy; QSurface3DSeries *m_series; }MyClass.cpp
MyClass::MyClass(QWidget*parent) : QMainWindow(parent) { ui.setupUi(this); m_graph = new Q3DSurface(); m_graphWidget = QWidget::createWindowContainer(m_graph); ui.verticalLayout->addWidget(m_graphWidget); m_model= new MyAbstractListModel(); m_proxy= new QItemModelSurfaceDataProxy(m_model, QStringLiteral("radius"), QStringLiteral("angle"), QStringLiteral("value")); m_series = new QSurface3DSeries(m_proxy); m_graph->addSeries(m_series); fillModelWithData(); } MyClass::~MyClass() { // do i need to destroy something? }Thanks !
-
Hi!
m_graphWidget = QWidget::createWindowContainer(m_graph);The container
m_graphWidgettakes ownership ofm_graph.ui.verticalLayout->addWidget(m_graphWidget);This will make
m_graphWidgeta child of the widget that holdsverticalLayout.m_model= new MyAbstractListModel();m_modeldoesn't have a parent.m_proxy= new QItemModelSurfaceDataProxy(m_model, QStringLiteral("radius"), QStringLiteral("angle"), QStringLiteral("value"));The proxy doesn't take ownership of the itemModel and you don't give a parent to
QItemModelSurfaceDataProxy().m_series = new QSurface3DSeries(m_proxy); m_graph->addSeries(m_series);You don't pass a parent to
QSurface3DSeries().
Looking at the docs it's not clear to me whether or notaddSeriesreparents it. But it wouldn't matter anyways if you'd pass a parent toQSurface3DSeries().------------------------------
m_graphandm_graphWidgetalready get destroyed automatically- Pass a parent to
MyAbstractListModel() - Pass a parent to
QItemModelSurfaceDataProxy() - Pass a parent to
QSurface3DSeries()
-
Hi!
m_graphWidget = QWidget::createWindowContainer(m_graph);The container
m_graphWidgettakes ownership ofm_graph.ui.verticalLayout->addWidget(m_graphWidget);This will make
m_graphWidgeta child of the widget that holdsverticalLayout.m_model= new MyAbstractListModel();m_modeldoesn't have a parent.m_proxy= new QItemModelSurfaceDataProxy(m_model, QStringLiteral("radius"), QStringLiteral("angle"), QStringLiteral("value"));The proxy doesn't take ownership of the itemModel and you don't give a parent to
QItemModelSurfaceDataProxy().m_series = new QSurface3DSeries(m_proxy); m_graph->addSeries(m_series);You don't pass a parent to
QSurface3DSeries().
Looking at the docs it's not clear to me whether or notaddSeriesreparents it. But it wouldn't matter anyways if you'd pass a parent toQSurface3DSeries().------------------------------
m_graphandm_graphWidgetalready get destroyed automatically- Pass a parent to
MyAbstractListModel() - Pass a parent to
QItemModelSurfaceDataProxy() - Pass a parent to
QSurface3DSeries()
@Wieland
Thanks for the informations!In the Surface Example they only delete the Q3DSurface instance.
But i will pass the parents to the classes, so i can be sure, that they are destroyed. Thanks!