From 7a77eeee06292dccf412b624a47a45833e6306d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sun, 10 Mar 2019 23:26:25 +0100 Subject: [PATCH] Fix NewFileDialog leaking MainWindow This also fixes segfaults on quitting when using plugins --- src/core/MainWindow.cpp | 2 +- src/dialogs/InitialOptionsDialog.cpp | 3 +-- src/dialogs/NewFileDialog.cpp | 8 ++++---- src/dialogs/NewFileDialog.h | 6 +++++- src/plugins/sample-python/sample_python.py | 12 ++++++------ src/widgets/CutterDockWidget.cpp | 8 ++++---- src/widgets/CutterDockWidget.h | 2 +- 7 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/core/MainWindow.cpp b/src/core/MainWindow.cpp index 6fba1d11..87392130 100644 --- a/src/core/MainWindow.cpp +++ b/src/core/MainWindow.cpp @@ -472,7 +472,7 @@ void MainWindow::displayWelcomeDialog() void MainWindow::displayNewFileDialog() { - NewFileDialog *n = new NewFileDialog(); + NewFileDialog *n = new NewFileDialog(this); newFileDialog = n; n->setAttribute(Qt::WA_DeleteOnClose); n->show(); diff --git a/src/dialogs/InitialOptionsDialog.cpp b/src/dialogs/InitialOptionsDialog.cpp index 2ef7cd74..b763ed98 100644 --- a/src/dialogs/InitialOptionsDialog.cpp +++ b/src/dialogs/InitialOptionsDialog.cpp @@ -423,6 +423,5 @@ void InitialOptionsDialog::on_scriptSelectButton_clicked() void InitialOptionsDialog::reject() { done(0); - NewFileDialog *n = new NewFileDialog(nullptr); - n->show(); + main->displayNewFileDialog(); } diff --git a/src/dialogs/NewFileDialog.cpp b/src/dialogs/NewFileDialog.cpp index 7d186e4b..580733da 100644 --- a/src/dialogs/NewFileDialog.cpp +++ b/src/dialogs/NewFileDialog.cpp @@ -50,9 +50,10 @@ static QIcon getIconFor(const QString &str, int pos) return QIcon(pixmap); } -NewFileDialog::NewFileDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::NewFileDialog) +NewFileDialog::NewFileDialog(MainWindow *main) : + QDialog(main), + ui(new Ui::NewFileDialog), + main(main) { ui->setupUi(this); setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint)); @@ -359,7 +360,6 @@ void NewFileDialog::loadFile(const QString &filename) settings.setValue("recentFileList", files); // Close dialog and open MainWindow/InitialOptionsDialog - MainWindow *main = new MainWindow(); QString ioFile = ""; if (ui->ioPlugin->currentIndex()) { ioFile = ui->ioPlugin->currentText() + "://"; diff --git a/src/dialogs/NewFileDialog.h b/src/dialogs/NewFileDialog.h index e89ac3fb..d8842d0a 100644 --- a/src/dialogs/NewFileDialog.h +++ b/src/dialogs/NewFileDialog.h @@ -9,12 +9,14 @@ namespace Ui { class NewFileDialog; } +class MainWindow; + class NewFileDialog : public QDialog { Q_OBJECT public: - explicit NewFileDialog(QWidget *parent = nullptr); + explicit NewFileDialog(MainWindow *main); ~NewFileDialog(); private slots: @@ -46,6 +48,8 @@ protected: private: std::unique_ptr ui; + MainWindow *main; + /** * @return true if list is not empty */ diff --git a/src/plugins/sample-python/sample_python.py b/src/plugins/sample-python/sample_python.py index cec6bbd4..8dfb6fb0 100644 --- a/src/plugins/sample-python/sample_python.py +++ b/src/plugins/sample-python/sample_python.py @@ -6,8 +6,8 @@ from PySide2.QtWidgets import QAction, QVBoxLayout, QLabel, QWidget, QSizePolicy class FortuneWidget(cutter.CutterDockWidget): - def __init__(self, main, action): - super(FortuneWidget, self).__init__(main, action) + def __init__(self, parent, action): + super(FortuneWidget, self).__init__(parent, action) self.setObjectName("FancyDockWidgetFromCoolPlugin") self.setWindowTitle("Sample Python Plugin") @@ -54,10 +54,10 @@ class CutterSamplePlugin(cutter.CutterPlugin): pass def setupInterface(self, main): - self.action = QAction("Sample Python Plugin", main) - self.action.setCheckable(True) - self.widget = FortuneWidget(main, self.action) # we MUST keep a reference to this! - main.addPluginDockWidget(self.widget, self.action) + action = QAction("Sample Python Plugin", main) + action.setCheckable(True) + widget = FortuneWidget(main, action) + main.addPluginDockWidget(widget, action) # This function will be called by Cutter and should return an instance of the plugin. diff --git a/src/widgets/CutterDockWidget.cpp b/src/widgets/CutterDockWidget.cpp index c66fcee8..e77a9c7b 100644 --- a/src/widgets/CutterDockWidget.cpp +++ b/src/widgets/CutterDockWidget.cpp @@ -2,13 +2,13 @@ #include "core/MainWindow.h" -CutterDockWidget::CutterDockWidget(MainWindow *main, QAction *action) : - QDockWidget(main), +CutterDockWidget::CutterDockWidget(MainWindow *parent, QAction *action) : + QDockWidget(parent), action(action) { if (action) { - main->addToDockWidgetList(this); - main->addDockWidgetAction(this, action); + parent->addToDockWidgetList(this); + parent->addDockWidgetAction(this, action); connect(action, &QAction::triggered, this, &CutterDockWidget::toggleDockWidget); } diff --git a/src/widgets/CutterDockWidget.h b/src/widgets/CutterDockWidget.h index 958f92f0..88f9b5e9 100644 --- a/src/widgets/CutterDockWidget.h +++ b/src/widgets/CutterDockWidget.h @@ -12,7 +12,7 @@ class CutterDockWidget : public QDockWidget Q_OBJECT public: - explicit CutterDockWidget(MainWindow *main, QAction *action = nullptr); + explicit CutterDockWidget(MainWindow *parent, QAction *action = nullptr); ~CutterDockWidget() override; bool eventFilter(QObject *object, QEvent *event) override; bool isVisibleToUser() { return isVisibleToUserCurrent; }