How to proper restore a FramelessWindow position after it get maximized/restored?
-
I'm writing some functions to
move
,maximize
, andrestore
a frameless window built usingQt
.When the window gets maximized by double clicking on it, and the user tries to drag it, I'm not figuring out how to properly return it to her previous position.
Example:
class MainWindow: public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow() {}; QWidget *titlebar_widget; bool mMoving; bool maximized; QPoint mLastMousePosition; QPoint mLastMousePositionBeforeMaximize; void mousePressEvent(QMouseEvent* event) { if (!titlebar_widget->underMouse()) return; if (event->button() == Qt::LeftButton) { mMoving = true; mLastMousePosition = event->pos(); } } void mouseMoveEvent(QMouseEvent* event) { if (!titlebar_widget->underMouse()) return; if ((maximized) && (event->buttons().testFlag(Qt::LeftButton) && mMoving)) { slot_restored(); maximized = false; mLastMousePosition = mLastMousePositionBeforeMaximize; return; } if (event->buttons().testFlag(Qt::LeftButton) && mMoving) this->move(this->pos() + (event->pos() - mLastMousePosition)); } void mouseReleaseEvent(QMouseEvent* event) { if (!titlebar_widget->underMouse()) return; if (event->button() == Qt::LeftButton) mMoving = false; } void mouseDoubleClickEvent(QMouseEvent* event) { Q_UNUSED(event); if (!titlebar_widget->underMouse()) return; if (event->button() != Qt::LeftButton) return; maximized = !maximized; if (maximized) { mLastMousePositionBeforeMaximize = event->pos(); slot_maximized(); } else slot_restored(); } void slot_minimized() { setWindowState(Qt::WindowMinimized); } void slot_restored() { setWindowState(Qt::WindowNoState); } void slot_maximized() { setWindowState(Qt::WindowMaximized); } private: Ui::MainWindowClass ui; };
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); ui.centralWidget->setStyleSheet("background-color: rgba(255, 255, 255, 155);"); titlebar_widget = ui.centralWidget; setAttribute(Qt::WA_TranslucentBackground); setWindowFlags(Qt::FramelessWindowHint); }
To be more precise my question lies here:
// The window is maximized, user has leftbutton down, and is // trying to move it. if ((maximized) && (event->buttons().testFlag(Qt::LeftButton) && mMoving)) { slot_restored(); maximized = false; mLastMousePosition = mLastMousePositionBeforeMaximize; }
Then the window is restored to her previous pos/size
slot_restored();
and the following code moves the window around:if (event->buttons().testFlag(Qt::LeftButton) && mMoving) this->move(this->pos() + (event->pos() - mLastMousePosition));
But at this point I'm getting a weird movement, its moving the window to a different position than it was before:
https://i.imgur.com/D3tTWz1.gif
In the gif isn't much noticeable because of the fps, but the window is jumping around.
How to properly calculate her position when it get restore by dragging?
-
Hello!
You can try to store the window size and position using the
saveGeometry
method (https://doc.qt.io/qt-6/qwidget.html#saveGeometry) and then you can callrestoreGeometry
(https://doc.qt.io/qt-6/qwidget.html#restoreGeometry) method to restore it. Happy coding!Merry Christmas!