Skip to content

Commit 2bcd730

Browse files
authored
Merge pull request #47 from uklotzde/widgethelper_crash
Adopt windowForWidget() from qtbase
2 parents 9cc7236 + 94b6b90 commit 2bcd730

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/util/widgethelper.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@ QPoint mapPopupToScreen(
1313
const QWidget& widget,
1414
const QPoint& popupUpperLeft,
1515
const QSize& popupSize) {
16-
const QWindow* window = widget.windowHandle();
17-
VERIFY_OR_DEBUG_ASSERT(window) {
16+
const auto* pWindow = getWindow(widget);
17+
if (!pWindow) {
1818
return popupUpperLeft;
1919
}
20-
const QScreen* screen = window->screen();
21-
VERIFY_OR_DEBUG_ASSERT(screen) {
22-
return popupUpperLeft;
23-
}
24-
const QSize screenSize = screen->size();
20+
const auto screenSize = pWindow->screen()->size();
2521
// math_clamp() cannot be used, because if the dimensions of
2622
// the popup menu are greater than the screen size a debug
2723
// assertion would be triggered!
@@ -36,6 +32,17 @@ QPoint mapPopupToScreen(
3632
return QPoint(adjustedX, adjustedY);
3733
}
3834

35+
QWindow* getWindow(
36+
const QWidget& widget) {
37+
if (auto* window = widget.windowHandle()) {
38+
return window;
39+
}
40+
if (auto* nativeParent = widget.nativeParentWidget()) {
41+
return nativeParent->windowHandle();
42+
}
43+
return nullptr;
44+
}
45+
3946
} // namespace widgethelper
4047

4148
} // namespace mixxx

src/util/widgethelper.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ QPoint mapPopupToScreen(
1616
const QPoint& popupUpperLeft,
1717
const QSize& popupSize);
1818

19+
/// Obtains the corresponding window for the given widget.
20+
///
21+
/// Might return nullptr if no window could be determined.
22+
///
23+
/// Adopted from windowForWidget() in qtbase/src/widgets/kernel/qapplication_p.h
24+
QWindow* getWindow(
25+
const QWidget& widget);
26+
1927
} // namespace widgethelper
2028

2129
} // namespace mixxx

0 commit comments

Comments
 (0)