Skip to content

Commit

Permalink
fix QMenu delete crash issue in WebViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
tamlok committed Jun 11, 2024
1 parent bfb0e5b commit 2ebb210
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/widgets/editors/markdownviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ void MarkdownViewer::setPreviewHelper(PreviewHelper *p_previewHelper)
void MarkdownViewer::contextMenuEvent(QContextMenuEvent *p_event)
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
QScopedPointer<QMenu> menu(page()->createStandardContextMenu());
QMenu* menu(page()->createStandardContextMenu());
#else
QScopedPointer<QMenu> menu(createStandardContextMenu());
QMenu* menu(createStandardContextMenu());
#endif

const QList<QAction *> actions = menu->actions();
Expand All @@ -133,7 +133,7 @@ void MarkdownViewer::contextMenuEvent(QContextMenuEvent *p_event)

if (!hasSelection() && m_viewWindow && m_viewWindow->getMode() == ViewWindowMode::Read) {
auto firstAct = actions.isEmpty() ? nullptr : actions[0];
auto editAct = new QAction(tr("&Edit"), menu.data());
auto editAct = new QAction(tr("&Edit"), menu);
WidgetUtils::addActionShortcutText(editAct,
ConfigMgr::getInst().getEditorConfig().getShortcut(EditorConfig::Shortcut::EditRead));
connect(editAct, &QAction::triggered,
Expand All @@ -151,7 +151,7 @@ void MarkdownViewer::contextMenuEvent(QContextMenuEvent *p_event)
{
auto defaultCopyImageAct = pageAction(QWebEnginePage::CopyImageToClipboard);
if (actions.contains(defaultCopyImageAct)) {
QAction *copyImageAct = new QAction(defaultCopyImageAct->text(), menu.data());
QAction *copyImageAct = new QAction(defaultCopyImageAct->text(), menu);
copyImageAct->setToolTip(defaultCopyImageAct->toolTip());
connect(copyImageAct, &QAction::triggered,
this, &MarkdownViewer::copyImage);
Expand All @@ -163,11 +163,11 @@ void MarkdownViewer::contextMenuEvent(QContextMenuEvent *p_event)
{
auto copyAct = pageAction(QWebEnginePage::Copy);
if (actions.contains(copyAct)) {
setupCrossCopyMenu(menu.data(), copyAct);
setupCrossCopyMenu(menu, copyAct);
}
}

hideUnusedActions(menu.data());
hideUnusedActions(menu);

p_event->accept();

Expand All @@ -183,6 +183,11 @@ void MarkdownViewer::contextMenuEvent(QContextMenuEvent *p_event)
if (valid) {
menu->exec(p_event->globalPos());
}

// For Qt 6, the menu is set with WA_DeleteOnClose.
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
delete menu;
#endif
}

void MarkdownViewer::handleCopyImageUrlAction()
Expand Down

0 comments on commit 2ebb210

Please sign in to comment.