Skip to content

Commit

Permalink
Merge pull request #47 from uklotzde/widgethelper_crash
Browse files Browse the repository at this point in the history
Adopt windowForWidget() from qtbase
  • Loading branch information
Be-ing authored Jul 7, 2020
2 parents 9cc7236 + 94b6b90 commit 2bcd730
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/util/widgethelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ QPoint mapPopupToScreen(
const QWidget& widget,
const QPoint& popupUpperLeft,
const QSize& popupSize) {
const QWindow* window = widget.windowHandle();
VERIFY_OR_DEBUG_ASSERT(window) {
const auto* pWindow = getWindow(widget);
if (!pWindow) {
return popupUpperLeft;
}
const QScreen* screen = window->screen();
VERIFY_OR_DEBUG_ASSERT(screen) {
return popupUpperLeft;
}
const QSize screenSize = screen->size();
const auto screenSize = pWindow->screen()->size();
// math_clamp() cannot be used, because if the dimensions of
// the popup menu are greater than the screen size a debug
// assertion would be triggered!
Expand All @@ -36,6 +32,17 @@ QPoint mapPopupToScreen(
return QPoint(adjustedX, adjustedY);
}

QWindow* getWindow(
const QWidget& widget) {
if (auto* window = widget.windowHandle()) {
return window;
}
if (auto* nativeParent = widget.nativeParentWidget()) {
return nativeParent->windowHandle();
}
return nullptr;
}

} // namespace widgethelper

} // namespace mixxx
8 changes: 8 additions & 0 deletions src/util/widgethelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ QPoint mapPopupToScreen(
const QPoint& popupUpperLeft,
const QSize& popupSize);

/// Obtains the corresponding window for the given widget.
///
/// Might return nullptr if no window could be determined.
///
/// Adopted from windowForWidget() in qtbase/src/widgets/kernel/qapplication_p.h
QWindow* getWindow(
const QWidget& widget);

} // namespace widgethelper

} // namespace mixxx

0 comments on commit 2bcd730

Please sign in to comment.