Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion docs/guide/data-flow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ Add interactive controls to your nodes:
class NumberSourceNode : public NodeDelegateModel
{
public:
~NumberSourceNode() override { delete _spinBox; }

QWidget* embeddedWidget() override
{
if (!_spinBox) {
Expand All @@ -235,14 +237,34 @@ Add interactive controls to your nodes:
}

private:
QDoubleSpinBox* _spinBox = nullptr;
QPointer<QDoubleSpinBox> _spinBox;
std::shared_ptr<NumberData> _data;
};

.. image:: /_static/screenshots/embedded-widget.png
:alt: Node with embedded spin box
:width: 400px

Widget ownership
~~~~~~~~~~~~~~~~

Two objects can own the widget, and either can be destroyed first:

- The model creates the widget. When the node is shown, its ``QGraphicsProxyWidget`` takes
it over and deletes it with the node.
- A widget that is never shown has no other owner. The registry builds a temporary model to
read its ``name()``. If the constructor creates the widget, only the model can free it.

Follow these rules:

- Create the widget lazily in ``embeddedWidget()``, as above. The temporary model never calls
it, so it never makes a widget.
- Delete the widget in the model destructor. This frees a widget that was never shown.
- Keep the widget in a ``QPointer``. A ``QPointer`` does not delete anything. It follows the
``QObject`` and becomes null when the object is destroyed, whoever destroys it. If the proxy
has already deleted the widget, the destructor ``delete`` does nothing. With a raw pointer
it would delete the widget twice and crash.

Validation State
----------------

Expand Down
5 changes: 5 additions & 0 deletions examples/calculator/NumberDisplayDataModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ NumberDisplayDataModel::NumberDisplayDataModel()
: _label{nullptr}
{}

NumberDisplayDataModel::~NumberDisplayDataModel()
{
delete _label;
}

unsigned int NumberDisplayDataModel::nPorts(PortType portType) const
{
unsigned int result = 1;
Expand Down
5 changes: 3 additions & 2 deletions examples/calculator/NumberDisplayDataModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QtNodes/NodeDelegateModel>

#include <QtCore/QObject>
#include <QtCore/QPointer>

#include <iostream>

Expand All @@ -25,7 +26,7 @@ class NumberDisplayDataModel : public NodeDelegateModel
public:
NumberDisplayDataModel();

~NumberDisplayDataModel() = default;
~NumberDisplayDataModel();

public:
QString caption() const override { return QStringLiteral("Result"); }
Expand All @@ -50,5 +51,5 @@ class NumberDisplayDataModel : public NodeDelegateModel
private:
std::shared_ptr<DecimalData> _numberData;

QLabel *_label;
QPointer<QLabel> _label;
};
5 changes: 5 additions & 0 deletions examples/calculator/NumberSourceDataModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ NumberSourceDataModel::NumberSourceDataModel()
, _number(std::make_shared<DecimalData>(0.0))
{}

NumberSourceDataModel::~NumberSourceDataModel()
{
delete _lineEdit;
}

QJsonObject NumberSourceDataModel::save() const
{
QJsonObject modelJson = NodeDelegateModel::save();
Expand Down
5 changes: 3 additions & 2 deletions examples/calculator/NumberSourceDataModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QtNodes/NodeDelegateModel>

#include <QtCore/QObject>
#include <QtCore/QPointer>

#include <iostream>

Expand All @@ -25,7 +26,7 @@ class NumberSourceDataModel : public NodeDelegateModel
public:
NumberSourceDataModel();

virtual ~NumberSourceDataModel() {}
virtual ~NumberSourceDataModel();

public:
QString caption() const override { return QStringLiteral("Number Source"); }
Expand Down Expand Up @@ -60,5 +61,5 @@ private Q_SLOTS:
private:
std::shared_ptr<DecimalData> _number;

QLineEdit *_lineEdit;
QPointer<QLineEdit> _lineEdit;
};
5 changes: 4 additions & 1 deletion examples/node_validation/TextDisplayModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QtNodes/NodeDelegateModel>

#include <QtCore/QObject>
#include <QtCore/QPointer>
#include <QtWidgets/QLabel>

#include "TextData.hpp"
Expand All @@ -23,6 +24,8 @@ class TextDisplayModel : public NodeDelegateModel
: _label(nullptr)
{}

~TextDisplayModel() override { delete _label; }

QString caption() const override { return QStringLiteral("Text Display"); }

QString name() const override { return QStringLiteral("TextDisplay"); }
Expand Down Expand Up @@ -66,5 +69,5 @@ class TextDisplayModel : public NodeDelegateModel
}

private:
QLabel *_label;
QPointer<QLabel> _label;
};
5 changes: 4 additions & 1 deletion examples/node_validation/TextSourceModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QtNodes/NodeDelegateModel>

#include <QtCore/QObject>
#include <QtCore/QPointer>
#include <QtWidgets/QLineEdit>

#include "TextData.hpp"
Expand All @@ -23,6 +24,8 @@ class TextSourceModel : public NodeDelegateModel
: _lineEdit(nullptr)
{}

~TextSourceModel() override { delete _lineEdit; }

QString caption() const override { return QStringLiteral("Text Source"); }

QString name() const override { return QStringLiteral("TextSource"); }
Expand Down Expand Up @@ -65,5 +68,5 @@ private Q_SLOTS:

private:
std::shared_ptr<TextData> _data;
QLineEdit *_lineEdit;
QPointer<QLineEdit> _lineEdit;
};
8 changes: 6 additions & 2 deletions examples/node_validation/ValidatedModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QtNodes/NodeDelegateModel>

#include <QtCore/QObject>
#include <QtCore/QPointer>
#include <QtCore/QTimer>
#include <QtWidgets/QLabel>
#include <QtWidgets/QVBoxLayout>
Expand Down Expand Up @@ -31,6 +32,9 @@ class ValidatedModel : public NodeDelegateModel
, _minLength(3)
{}

// The label is a child of the widget, so deleting the widget deletes it.
~ValidatedModel() override { delete _widget; }

QString caption() const override { return QStringLiteral("Text Validator"); }

QString name() const override { return QStringLiteral("TextValidator"); }
Expand Down Expand Up @@ -152,7 +156,7 @@ class ValidatedModel : public NodeDelegateModel

private:
std::shared_ptr<TextData> _outputData;
QWidget *_widget;
QLabel *_label;
QPointer<QWidget> _widget;
QPointer<QLabel> _label;
int _minLength;
};
5 changes: 3 additions & 2 deletions examples/resizable_images/ImageLoaderModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>

#include <QtCore/QObject>
#include <QtCore/QPointer>
#include <QtWidgets/QLabel>

#include <QtNodes/NodeDelegateModel>
Expand All @@ -25,7 +26,7 @@ class ImageLoaderModel : public NodeDelegateModel
public:
ImageLoaderModel();

~ImageLoaderModel() = default;
~ImageLoaderModel() override { delete _label; }

public:
QString caption() const override { return QString("Image Source"); }
Expand All @@ -51,7 +52,7 @@ class ImageLoaderModel : public NodeDelegateModel
bool eventFilter(QObject *object, QEvent *event) override;

private:
QLabel *_label;
QPointer<QLabel> _label;

QPixmap _pixmap;
};
5 changes: 3 additions & 2 deletions examples/resizable_images/ImageShowModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>

#include <QtCore/QObject>
#include <QtCore/QPointer>
#include <QtWidgets/QLabel>

#include <QtNodes/NodeDelegateModel>
Expand All @@ -23,7 +24,7 @@ class ImageShowModel : public NodeDelegateModel
public:
ImageShowModel();

~ImageShowModel() = default;
~ImageShowModel() override { delete _label; }

public:
QString caption() const override { return QString("Image Display"); }
Expand All @@ -49,7 +50,7 @@ class ImageShowModel : public NodeDelegateModel
bool eventFilter(QObject *object, QEvent *event) override;

private:
QLabel *_label;
QPointer<QLabel> _label;

std::shared_ptr<NodeData> _nodeData;
};
5 changes: 3 additions & 2 deletions examples/text/TextDisplayDataModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QtNodes/NodeDelegateModel>

#include <QtCore/QObject>
#include <QtCore/QPointer>
#include <QtWidgets/QLabel>

#include <iostream>
Expand All @@ -25,7 +26,7 @@ class TextDisplayDataModel : public NodeDelegateModel
public:
TextDisplayDataModel();

virtual ~TextDisplayDataModel() {}
virtual ~TextDisplayDataModel() { delete _label; }

public:
QString caption() const override { return QString("Text Display"); }
Expand All @@ -48,6 +49,6 @@ class TextDisplayDataModel : public NodeDelegateModel
QWidget *embeddedWidget() override { return _label; }

private:
QLabel *_label;
QPointer<QLabel> _label;
QString _inputText;
};
5 changes: 5 additions & 0 deletions examples/text/TextSourceDataModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ TextSourceDataModel::TextSourceDataModel()
//
}

TextSourceDataModel::~TextSourceDataModel()
{
delete _lineEdit;
}

unsigned int TextSourceDataModel::nPorts(PortType portType) const
{
unsigned int result = 1;
Expand Down
5 changes: 4 additions & 1 deletion examples/text/TextSourceDataModel.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <QtCore/QObject>
#include <QtCore/QPointer>

#include "TextData.hpp"

Expand All @@ -24,6 +25,8 @@ class TextSourceDataModel : public NodeDelegateModel
public:
TextSourceDataModel();

~TextSourceDataModel() override;

public:
QString caption() const override { return QString("Text Source"); }

Expand All @@ -49,5 +52,5 @@ private Q_SLOTS:
void onTextEdited(QString const &string);

private:
QLineEdit *_lineEdit;
QPointer<QLineEdit> _lineEdit;
};
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_executable(test_nodes
src/TestBasicGraphicsScene.cpp
src/TestNodeGroup.cpp
src/TestNodeNickname.cpp
src/TestEmbeddedWidgetOwnership.cpp
src/TestUIInteraction.cpp
src/TestNodeDrag.cpp
src/TestDataFlow.cpp
Expand Down
Loading
Loading