From ec0b4e31e16515dea2cc006d50bde775445dd352 Mon Sep 17 00:00:00 2001 From: liuminghang Date: Fri, 11 Jun 2021 11:38:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E7=AA=97=E5=8F=A3=E5=B4=A9=E6=BA=83=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 由于之前有多的弹簧new出未释放 Log: 与主窗口有关 Change-Id: I4c9f0036ac3486b31016f7654cd646bd6a91e211 --- src/mainwidget.cpp | 89 ++++++++++++++++++++++++++++------------------ src/mainwindow.cpp | 1 + 2 files changed, 55 insertions(+), 35 deletions(-) diff --git a/src/mainwidget.cpp b/src/mainwidget.cpp index f9c77dd..9871128 100644 --- a/src/mainwidget.cpp +++ b/src/mainwidget.cpp @@ -30,7 +30,9 @@ MainWidget::MainWidget(QWidget *parent) : MainWidget::~MainWidget() { // m_mainGridLayout->addLayout(m_buttonHorizontalLayout, 1, 0, 1, 1); - m_mainGridLayout->removeItem(m_buttonHorizontalLayout); + if (m_mainGridLayout && m_buttonHorizontalLayout) { + m_mainGridLayout->removeItem(m_buttonHorizontalLayout); + } if (m_imageview) { m_imageview->deleteLater(); m_imageview = nullptr; @@ -107,7 +109,7 @@ void MainWidget::setupUi(QWidget *Widget) m_buttonHorizontalLayout = new QHBoxLayout(Widget); m_buttonHorizontalLayout->setContentsMargins(20, 0, 59, 0); //表示控件与窗体的左右边距 // m_buttonHorizontalLayout->setSpacing(30); - m_buttonHorizontalLayout->setObjectName(QStringLiteral("horizontalLayout_2")); + m_tipHorizontalLayout = new QHBoxLayout(Widget); @@ -122,7 +124,7 @@ void MainWidget::setupUi(QWidget *Widget) m_tipHorizontalLayout->addWidget(m_tiplabel); m_buttonHorizontalLayout->addLayout(m_tipHorizontalLayout); - QSpacerItem *horizontalSpacer = new QSpacerItem(159, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); m_buttonHorizontalLayout->addItem(horizontalSpacer); + QSpacerItem *horizontalSpacer = new QSpacerItem(159, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); m_buttonHorizontalLayout->addItem(horizontalSpacer); m_copyBtn = new DIconButton(Widget); @@ -178,11 +180,13 @@ void MainWidget::createLoadingUi() m_loadingWidget->show(); m_loadingTip = new DLabel(tr("Recognizing"), this); m_loadingTip->show(); - DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType(); - if (themeType == DGuiApplicationHelper::DarkType) { - m_imageview->setForegroundBrush(QColor(0, 0, 0, 150)); //设置场景的前景色,类似于遮罩 - } else { - m_imageview->setForegroundBrush(QColor(255, 255, 255, 150)); //设置场景的前景色,类似于遮罩 + if (m_imageview) { + DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType(); + if (themeType == DGuiApplicationHelper::DarkType) { + m_imageview->setForegroundBrush(QColor(0, 0, 0, 150)); //设置场景的前景色,类似于遮罩 + } else { + m_imageview->setForegroundBrush(QColor(255, 255, 255, 150)); //设置场景的前景色,类似于遮罩 + } } } @@ -212,17 +216,22 @@ void MainWidget::loadingUi() void MainWidget::openImage(const QString &path) { - QImage img(path); - m_imageview->openFilterImage(img); - m_imageview->fitWindow(); - m_imgName = path; + if (m_imageview) { + QImage img(path); + m_imageview->openFilterImage(img); + m_imageview->fitWindow(); + m_imgName = path; + } + } void MainWidget::openImage(const QImage &img) { - m_imageview->openFilterImage(img); - m_imageview->fitWindow(); - m_imgName = ""; + if (m_imageview) { + m_imageview->openFilterImage(img); + m_imageview->fitWindow(); + m_imgName = ""; + } } void MainWidget::resizeEvent(QResizeEvent *event) @@ -272,43 +281,53 @@ void MainWidget::slotExport() void MainWidget::setIcons(DGuiApplicationHelper::ColorType themeType) { if (themeType == DGuiApplicationHelper::DarkType) { - m_tipIconLabel->setPixmap(QPixmap(":/assets/tip_dark.svg")); - m_tipIconLabel->setFixedSize(QSize(14, 14)); + if (m_tipIconLabel) { + m_tipIconLabel->setPixmap(QPixmap(":/assets/tip_dark.svg")); + m_tipIconLabel->setFixedSize(QSize(14, 14)); + } - m_copyBtn->setIcon(QIcon(":/assets/copy_dark.svg")); - m_copyBtn->setIconSize(QSize(36, 36)); - m_copyBtn->setFlat(true); - - m_exportBtn->setIcon(QIcon(":/assets/download_dark.svg")); - m_exportBtn->setIconSize(QSize(36, 36)); - m_exportBtn->setFlat(true); + if (m_copyBtn) { + m_copyBtn->setIcon(QIcon(":/assets/copy_dark.svg")); + m_copyBtn->setIconSize(QSize(36, 36)); + m_copyBtn->setFlat(true); + } + if (m_exportBtn) { + m_exportBtn->setIcon(QIcon(":/assets/download_dark.svg")); + m_exportBtn->setIconSize(QSize(36, 36)); + m_exportBtn->setFlat(true); + } App->setWindowIcon(QIcon(":/assets/appicon_dark.svg")); DMainWindow *mainWindow = static_cast(this->parent()); if (mainWindow) { mainWindow->titlebar()->setIcon(QIcon(":/assets/appicon_dark.svg")); } - if (m_isLoading) { + if (m_isLoading && m_imageview) { m_imageview->setForegroundBrush(QColor(0, 0, 0, 150)); //设置场景的前景色,类似于遮罩 } } else { - m_tipIconLabel->setPixmap(QPixmap(":/assets/tip_light.svg")); - m_tipIconLabel->setFixedSize(QSize(14, 14)); + if (m_tipIconLabel) { + m_tipIconLabel->setPixmap(QPixmap(":/assets/tip_light.svg")); + m_tipIconLabel->setFixedSize(QSize(14, 14)); + } - m_copyBtn->setIcon(QIcon(":/assets/copy_light.svg")); - m_copyBtn->setIconSize(QSize(36, 36)); - m_copyBtn->setFlat(true); - - m_exportBtn->setIcon(QIcon(":/assets/download_light.svg")); - m_exportBtn->setIconSize(QSize(36, 36)); - m_exportBtn->setFlat(true); + if (m_copyBtn) { + m_copyBtn->setIcon(QIcon(":/assets/copy_light.svg")); + m_copyBtn->setIconSize(QSize(36, 36)); + m_copyBtn->setFlat(true); + } + if (m_exportBtn) { + m_exportBtn->setIcon(QIcon(":/assets/download_light.svg")); + m_exportBtn->setIconSize(QSize(36, 36)); + m_exportBtn->setFlat(true); + } App->setWindowIcon(QIcon(":/assets/appicon_light.svg")); DMainWindow *mainWindow = static_cast(this->parent()); if (mainWindow) { mainWindow->titlebar()->setIcon(QIcon(":/assets/appicon_light.svg")); } - if (m_isLoading) { + if (m_isLoading && m_imageview) { m_imageview->setForegroundBrush(QColor(255, 255, 255, 150)); //设置场景的前景色,类似于遮罩 } } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0976a68..4d41393 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -30,6 +30,7 @@ MainWindow::MainWindow(QWidget *parent) : DMainWindow(parent) { + setAttribute(Qt::WA_DeleteOnClose); if (!m_mainWidget) { m_mainWidget = new MainWidget(this); }