Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to proper restore a FramelessWindow position after it get maximized/restored?
QtWS25 Last Chance

How to proper restore a FramelessWindow position after it get maximized/restored?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 506 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nikollas
    wrote on last edited by
    #1

    I'm writing some functions to move, maximize, and restore a frameless window built using Qt.

    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?

    1 Reply Last reply
    0
    • N Offline
      N Offline
      Nikollas
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • N Offline
        N Offline
        Nikollas
        wrote on last edited by
        #3

        Still couldnt find how to proper store/restore the position in this case. Im using windows 10 and qt6.4

        1 Reply Last reply
        0
        • N Offline
          N Offline
          Nikollas
          wrote on last edited by
          #4

          Still lf help on this

          Cobra91151C 1 Reply Last reply
          0
          • N Nikollas

            Still lf help on this

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by
            #5

            @Nikollas

            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 call restoreGeometry (https://doc.qt.io/qt-6/qwidget.html#restoreGeometry) method to restore it. Happy coding!

            Merry Christmas!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved