From 81eeaa3a20b274d5f604a63e2807fd81e70be747 Mon Sep 17 00:00:00 2001 From: Simon Edwards Date: Sat, 16 Oct 2021 14:16:34 +0200 Subject: [PATCH] Don't delete QObjects which have a parent Qt's memory management is built around trees of objects where deleting the root object also deletes the whole tree. Objects with a parent are owned by that parent. --- src/cpp/include/nodegui/Extras/Utils/nutils.h | 2 +- src/lib/QtWidgets/QAction.ts | 4 ---- src/lib/QtWidgets/QPainter.ts | 6 +++--- src/lib/QtWidgets/QWidget.ts | 5 ----- 4 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/cpp/include/nodegui/Extras/Utils/nutils.h b/src/cpp/include/nodegui/Extras/Utils/nutils.h index 993ccee46..b6519b68f 100644 --- a/src/cpp/include/nodegui/Extras/Utils/nutils.h +++ b/src/cpp/include/nodegui/Extras/Utils/nutils.h @@ -20,7 +20,7 @@ DLL_EXPORT void* configureComponent(void* component); template void safeDelete(QPointer& component) { - if (!component.isNull()) { + if (!component.isNull() && component->QObject::parent() == nullptr) { delete component; } } diff --git a/src/lib/QtWidgets/QAction.ts b/src/lib/QtWidgets/QAction.ts index 61747fc0a..28772a8e7 100644 --- a/src/lib/QtWidgets/QAction.ts +++ b/src/lib/QtWidgets/QAction.ts @@ -32,8 +32,6 @@ menu.addAction(menuAction); */ export class QAction extends NodeObject { native: NativeElement; - icon?: QIcon; - menu?: QMenu; constructor(); constructor(native: NativeElement); constructor(parent: NodeWidget); @@ -56,11 +54,9 @@ export class QAction extends NodeObject { this.native.setEnabled(enabled); } setIcon(icon: QIcon): void { - this.icon = icon; this.native.setIcon(icon.native); } setMenu(menu: QMenu): void { - this.menu = menu; this.native.setMenu(menu.native); } setShortcut(keysequence: QKeySequence): void { diff --git a/src/lib/QtWidgets/QPainter.ts b/src/lib/QtWidgets/QPainter.ts index ba565421e..481d250ac 100644 --- a/src/lib/QtWidgets/QPainter.ts +++ b/src/lib/QtWidgets/QPainter.ts @@ -183,10 +183,10 @@ export class QPainter extends Component { // TODO: void setBackgroundMode(Qt::BGMode mode) setBrush(colorOrBrush: QColor | QBrush): void { if (colorOrBrush instanceof QColor) { - this.native.setBrush(colorOrBrush.native, 'color'); + this.native.setBrush(colorOrBrush.native, 'color'); } else { - this.native.setBrush(colorOrBrush.native, 'brush'); - } + this.native.setBrush(colorOrBrush.native, 'brush'); + } } setBrushOrigin(x: number, y: number): void { this.native.setBrushOrigin(x, y); diff --git a/src/lib/QtWidgets/QWidget.ts b/src/lib/QtWidgets/QWidget.ts index ba9f0ae0c..5891eb8a3 100644 --- a/src/lib/QtWidgets/QWidget.ts +++ b/src/lib/QtWidgets/QWidget.ts @@ -54,13 +54,11 @@ Implement all native QWidget methods here so that all widgets get access to thos */ export abstract class NodeWidget extends YogaWidget { _layout?: NodeLayout; - actions: Set; _rawInlineStyle = ''; type = 'widget'; private _effect?: QGraphicsEffect | null; constructor(native: NativeElement) { super(native); - this.actions = new Set(); this.setStyleSheet = memoizeOne(this.setStyleSheet); this.setInlineStyle = memoizeOne(this.setInlineStyle); this.setObjectName = memoizeOne(this.setObjectName); @@ -86,11 +84,9 @@ export abstract class NodeWidget extends YogaWid const qaction = new QAction(); qaction.setText(action); this.native.addAction(qaction.native); - this.actions.add(qaction); return qaction; } this.native.addAction(action.native); - this.actions.add(action); return action; } // TODO: void addActions(QList actions) @@ -222,7 +218,6 @@ export abstract class NodeWidget extends YogaWid // TODO: void releaseShortcut(int id) removeAction(action: QAction): void { this.native.removeAction(action.native); - this.actions.delete(action); } // TODO: void render(QPaintDevice *target, const QPoint &targetOffset = QPoint(), const QRegion &sourceRegion = QRegion(), QWidget::RenderFlags renderFlags = RenderFlags(DrawWindowBackground | DrawChildren)) // TODO: void render(QPainter *painter, const QPoint &targetOffset = QPoint(), const QRegion &sourceRegion = QRegion(), QWidget::RenderFlags renderFlags = RenderFlags(DrawWindowBackground | DrawChildren))