diff options
| author | Shawn Rutledge <shawn.rutledge@qt.io> | 2023-12-28 19:15:14 -0700 |
|---|---|---|
| committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2024-01-22 07:45:35 +0000 |
| commit | 684656d53a3064c61744921269d4dca25c08dc5b (patch) | |
| tree | b6513448b4ee2595073e94151348b985f11845f9 | |
| parent | d2c935b2c73439c8953cb06f04befebeba67746c (diff) | |
Avoid crashing in the quickwidgets example after closing the subwindow
Closing the window deletes m_quickWidget; so we disable the menu items
for grabbing from it, because those cannot work. Also add asserts.
Fixes: QTBUG-120296
Change-Id: I68154c2d1e4553c771815e29cbe3b095d85893f1
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
(cherry picked from commit f719ee6408a92439a65e82abea4403df6a844066)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit a49d309b4acaba1dbb08ea1c554ad333b439b8cf)
(cherry picked from commit 08c01f62593783b3c650e014a1112c0cd223710a)
(cherry picked from commit fadb14a8d91e064e172e5c6751b5b7d5a02d24dd)
(cherry picked from commit 67902f1a0852bbd69fce0f7d0d784b7495ac324b)
| -rw-r--r-- | examples/quick/quickwidgets/quickwidget/main.cpp | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/examples/quick/quickwidgets/quickwidget/main.cpp b/examples/quick/quickwidgets/quickwidget/main.cpp index f28b2f1443..1acb08bd1e 100644 --- a/examples/quick/quickwidgets/quickwidget/main.cpp +++ b/examples/quick/quickwidgets/quickwidget/main.cpp @@ -108,14 +108,22 @@ MainWindow::MainWindow() setCentralWidget(centralWidget); QMenu *fileMenu = menuBar()->addMenu(tr("&File")); - fileMenu->addAction(tr("Grab framebuffer"), this, &MainWindow::grabFramebuffer); - fileMenu->addAction(tr("Render to pixmap"), this, &MainWindow::renderToPixmap); - fileMenu->addAction(tr("Grab via grabToImage"), this, &MainWindow::grabToImage); + auto grabAction = fileMenu->addAction(tr("Grab framebuffer"), this, &MainWindow::grabFramebuffer); + auto renderAction = fileMenu->addAction(tr("Render to pixmap"), this, &MainWindow::renderToPixmap); + auto grabToImageAction = fileMenu->addAction(tr("Grab via grabToImage"), this, &MainWindow::grabToImage); fileMenu->addAction(tr("Quit"), qApp, &QCoreApplication::quit); QMenu *windowMenu = menuBar()->addMenu(tr("&Window")); windowMenu->addAction(tr("Add tab widget"), this, [this, centralWidget] { createQuickWidgetsInTabs(centralWidget); }); + + connect(m_quickWidget, &QObject::destroyed, this, + [this, grabAction, renderAction, grabToImageAction] { + m_quickWidget = nullptr; + grabAction->setEnabled(false); + renderAction->setEnabled(false); + grabToImageAction->setEnabled(false); + }); } void MainWindow::createQuickWidgetsInTabs(QMdiArea *mdiArea) @@ -146,6 +154,7 @@ void MainWindow::quickWidgetStatusChanged(QQuickWidget::Status status) { if (status == QQuickWidget::Error) { QStringList errors; + Q_ASSERT(m_quickWidget); const auto widgetErrors = m_quickWidget->errors(); for (const QQmlError &error : widgetErrors) errors.append(error.toString()); @@ -170,12 +179,14 @@ template<class T> void saveToFile(QWidget *parent, T *saveable) void MainWindow::grabFramebuffer() { + Q_ASSERT(m_quickWidget); QImage image = m_quickWidget->grabFramebuffer(); saveToFile(this, &image); } void MainWindow::renderToPixmap() { + Q_ASSERT(m_quickWidget); QPixmap pixmap(m_quickWidget->size()); m_quickWidget->render(&pixmap); saveToFile(this, &pixmap); @@ -188,6 +199,7 @@ void MainWindow::grabToImage() fd.setDefaultSuffix("png"); fd.selectFile("test_grabToImage.png"); if (fd.exec() == QDialog::Accepted) { + Q_ASSERT(m_quickWidget); QMetaObject::invokeMethod(m_quickWidget->rootObject(), "performLayerBasedGrab", Q_ARG(QVariant, fd.selectedFiles().first())); } |
