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.
This commit is contained in:
Simon Edwards 2021-10-16 14:16:34 +02:00
parent 47c00fabc1
commit 81eeaa3a20
4 changed files with 4 additions and 13 deletions

View File

@ -20,7 +20,7 @@ DLL_EXPORT void* configureComponent(void* component);
template <typename T>
void safeDelete(QPointer<T>& component) {
if (!component.isNull()) {
if (!component.isNull() && component->QObject::parent() == nullptr) {
delete component;
}
}

View File

@ -32,8 +32,6 @@ menu.addAction(menuAction);
*/
export class QAction extends NodeObject<QActionSignals> {
native: NativeElement;
icon?: QIcon;
menu?: QMenu;
constructor();
constructor(native: NativeElement);
constructor(parent: NodeWidget<any>);
@ -56,11 +54,9 @@ export class QAction extends NodeObject<QActionSignals> {
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 {

View File

@ -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);

View File

@ -54,13 +54,11 @@ Implement all native QWidget methods here so that all widgets get access to thos
*/
export abstract class NodeWidget<Signals extends QWidgetSignals> extends YogaWidget<Signals> {
_layout?: NodeLayout<Signals>;
actions: Set<QAction>;
_rawInlineStyle = '';
type = 'widget';
private _effect?: QGraphicsEffect<any> | null;
constructor(native: NativeElement) {
super(native);
this.actions = new Set<QAction>();
this.setStyleSheet = memoizeOne(this.setStyleSheet);
this.setInlineStyle = memoizeOne(this.setInlineStyle);
this.setObjectName = memoizeOne(this.setObjectName);
@ -86,11 +84,9 @@ export abstract class NodeWidget<Signals extends QWidgetSignals> 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<QAction *> actions)
@ -222,7 +218,6 @@ export abstract class NodeWidget<Signals extends QWidgetSignals> 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))