Don't rely on QCursor::pos() in dialog utils

It might not work under Wayland.
This commit is contained in:
Martchus 2019-07-20 18:34:58 +02:00
parent 6eaeaab442
commit 7cf1a57368
2 changed files with 10 additions and 10 deletions

View File

@ -116,25 +116,25 @@ QRect availableScreenGeometryAtPoint(const QPoint &point)
* \brief Moves the specified \a widget in the middle of the (available) screen * \brief Moves the specified \a widget in the middle of the (available) screen
* area or \a parent if specified. * area or \a parent if specified.
* *
* If there are multiple screens available, the screen where the cursor currently * The screen containing the current cursor position is used unless \a position
* is located is chosen. * is specified.
*/ */
void centerWidget(QWidget *widget, const QWidget *parent) void centerWidget(QWidget *widget, const QWidget *parent, const QPoint *position)
{ {
widget->setGeometry(QStyle::alignedRect( widget->setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, widget->size(),
Qt::LeftToRight, Qt::AlignCenter, widget->size(), parent ? parent->geometry() : availableScreenGeometryAtPoint(QCursor::pos()))); parent ? parent->geometry() : availableScreenGeometryAtPoint(position ? *position : QCursor::pos())));
} }
/*! /*!
* \brief Moves the specified \a widget to the corner which is closest to the * \brief Moves the specified \a widget to the corner which is closest to the
* current cursor position. * current cursor position or \a position if specified.
* *
* If there are multiple screens available, the screen where the cursor currently * If there are multiple screens available, the screen where the cursor currently
* is located is chosen. * is located is chosen.
*/ */
void cornerWidget(QWidget *widget) void cornerWidget(QWidget *widget, const QPoint *position)
{ {
const QPoint cursorPos(QCursor::pos()); const QPoint cursorPos(position ? *position : QCursor::pos());
const QRect availableGeometry(availableScreenGeometryAtPoint(cursorPos)); const QRect availableGeometry(availableScreenGeometryAtPoint(cursorPos));
Qt::Alignment alignment = nullptr; Qt::Alignment alignment = nullptr;
alignment |= (cursorPos.x() - availableGeometry.left() < availableGeometry.right() - cursorPos.x() ? Qt::AlignLeft : Qt::AlignRight); alignment |= (cursorPos.x() - availableGeometry.left() < availableGeometry.right() - cursorPos.x() ? Qt::AlignLeft : Qt::AlignRight);

View File

@ -36,8 +36,8 @@ QColor QT_UTILITIES_EXPORT instructionTextColor();
const QString QT_UTILITIES_EXPORT &dialogStyle(); const QString QT_UTILITIES_EXPORT &dialogStyle();
#ifdef QT_UTILITIES_GUI_QTWIDGETS #ifdef QT_UTILITIES_GUI_QTWIDGETS
QRect QT_UTILITIES_EXPORT availableScreenGeometryAtPoint(const QPoint &point); QRect QT_UTILITIES_EXPORT availableScreenGeometryAtPoint(const QPoint &point);
void QT_UTILITIES_EXPORT centerWidget(QWidget *widget, const QWidget *parent = nullptr); void QT_UTILITIES_EXPORT centerWidget(QWidget *widget, const QWidget *parent = nullptr, const QPoint *position = nullptr);
void QT_UTILITIES_EXPORT cornerWidget(QWidget *widget); void QT_UTILITIES_EXPORT cornerWidget(QWidget *widget, const QPoint *position = nullptr);
void QT_UTILITIES_EXPORT makeHeading(QWidget *widget); void QT_UTILITIES_EXPORT makeHeading(QWidget *widget);
void QT_UTILITIES_EXPORT updateStyle(QWidget *widget); void QT_UTILITIES_EXPORT updateStyle(QWidget *widget);
#endif #endif