Skip to content

Commit f89c504

Browse files
committed
core/menu: opening platform menus w/o QApplication no longer crashes
An error is displayed instead.
1 parent 1d2bf5d commit f89c504

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/core/platformmenu.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
#include <qaction.h>
66
#include <qactiongroup.h>
7+
#include <qapplication.h>
78
#include <qcontainerfwd.h>
9+
#include <qcoreapplication.h>
810
#include <qicon.h>
911
#include <qlogging.h>
1012
#include <qmenu.h>
@@ -74,7 +76,13 @@ void PlatformMenuEntry::registerCreationHook(std::function<void(PlatformMenuQMen
7476
bool PlatformMenuEntry::display(QObject* parentWindow, int relativeX, int relativeY) {
7577
QWindow* window = nullptr;
7678

77-
if (this->qmenu == nullptr) {
79+
if (qobject_cast<QApplication*>(QCoreApplication::instance()) == nullptr) {
80+
qCritical() << "Cannot display PlatformMenuEntry as quickshell was not started in "
81+
"QApplication mode.";
82+
qCritical() << "To use platform menus, add `//@ pragma UseQApplication` to the top of your "
83+
"root QML file and restart quickshell.";
84+
return false;
85+
} else if (this->qmenu == nullptr) {
7886
qCritical() << "Cannot display PlatformMenuEntry as it is not a menu.";
7987
return false;
8088
} else if (parentWindow == nullptr) {
@@ -113,7 +121,13 @@ bool PlatformMenuEntry::display(QObject* parentWindow, int relativeX, int relati
113121
}
114122

115123
bool PlatformMenuEntry::display(PopupAnchor* anchor) {
116-
if (!anchor->backingWindow() || !anchor->backingWindow()->isVisible()) {
124+
if (qobject_cast<QApplication*>(QCoreApplication::instance()) == nullptr) {
125+
qCritical() << "Cannot display PlatformMenuEntry as quickshell was not started in "
126+
"QApplication mode.";
127+
qCritical() << "To use platform menus, add `//@ pragma UseQApplication` to the top of your "
128+
"root QML file and restart quickshell.";
129+
return false;
130+
} else if (!anchor->backingWindow() || !anchor->backingWindow()->isVisible()) {
117131
qCritical() << "Cannot display PlatformMenuEntry on anchor without visible window.";
118132
return false;
119133
}
@@ -140,6 +154,10 @@ bool PlatformMenuEntry::display(PopupAnchor* anchor) {
140154
}
141155

142156
void PlatformMenuEntry::relayout() {
157+
if (qobject_cast<QApplication*>(QCoreApplication::instance()) == nullptr) {
158+
return;
159+
}
160+
143161
if (this->menu->hasChildren()) {
144162
delete this->qaction;
145163
this->qaction = nullptr;

src/core/qsmenuanchor.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "qsmenuanchor.hpp"
22

3+
#include <qapplication.h>
4+
#include <qcoreapplication.h>
35
#include <qlogging.h>
46
#include <qobject.h>
57
#include <qtmetamacros.h>
@@ -15,6 +17,14 @@ namespace qs::menu {
1517
QsMenuAnchor::~QsMenuAnchor() { this->onClosed(); }
1618

1719
void QsMenuAnchor::open() {
20+
if (qobject_cast<QApplication*>(QCoreApplication::instance()) == nullptr) {
21+
qCritical() << "Cannot call QsMenuAnchor.open() as quickshell was not started in "
22+
"QApplication mode.";
23+
qCritical() << "To use platform menus, add `//@ pragma UseQApplication` to the top of your "
24+
"root QML file and restart quickshell.";
25+
return;
26+
}
27+
1828
if (this->mOpen) {
1929
qCritical() << "Cannot call QsMenuAnchor.open() as it is already open.";
2030
return;

0 commit comments

Comments
 (0)