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. Docking and moving a frameless window?
QtWS25 Last Chance

Docking and moving a frameless window?

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++ qtframelesswindowmenubar
4 Posts 2 Posters 829 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.
  • S Offline
    S Offline
    StudentScripter
    wrote on 22 Mar 2024, 18:24 last edited by
    #1

    So i used the frameless window hint to remove the titlebar and implemented a custom menubar which lets me move, maximise, minimize and close the window.
    However when i move very fast the window sometimes gets stuck somehow, but i couldn't figure out why and what im doing wrong?
    Also i couldn't figure out how to make the window dock/snap when being dragged to the sides or far top or bottom:

    3668da15-e6af-4017-ab29-35256b7172d3-image.png

    #include "CustomMenubar.h"
    #include <QApplication>
    #include <QMouseEvent>
    
    CustomMenuBar::CustomMenuBar(QWidget *parent)
        : QMenuBar{parent}
    {
    
    
    }
    
    
    
    void CustomMenuBar::mousePressEvent(QMouseEvent *event)
    {
        QAction *action = actionAt(event->pos());
        if (action && action->menu()) {
            // If a MenuItem with a submenu is clicked, we won't start moving the window
            clickedOnMenuItem = true;
            isMoving = false;
            QMenuBar::mousePressEvent(event);
        } else {
            QMenuBar::mousePressEvent(event);
            pressPos = event->globalPosition();
            isMoving = true;
            event->accept();
        }
    
    
    }
    
    void CustomMenuBar::mouseReleaseEvent(QMouseEvent *event)
    {
        QAction *action = actionAt(event->pos());
        if (action && action->menu()) {
    
            QMenuBar::mouseReleaseEvent(event);
            return;
        }
    
        // If the mouse was being used for dragging, stop moving
        if (isMoving) {
            isMoving = false;
            event->accept();
        }
    }
    
    void CustomMenuBar::mouseMoveEvent(QMouseEvent *event)
    {
        QAction *action = actionAt(event->pos());
        if (action && action->menu()) {
    
            QMenuBar::mouseMoveEvent(event);
            return;
        }
    
    
        // If the mouse was pressed and is moving, move the window
        if (isMoving) {
            QMenuBar::mouseMoveEvent(event);
            QPointF diff = event->globalPosition() - pressPos;
            QWidget *window = QApplication::topLevelAt(pressPos.toPoint());
            if (window && window->isWindow()) {
                window->move(window->pos() + diff.toPoint());
                pressPos = event->globalPosition();
                event->accept();
            }
        }
    }
    
    

    Thanks in advance. :) Have a nice evening.

    P 1 Reply Last reply 22 Mar 2024, 19:32
    0
    • S StudentScripter
      22 Mar 2024, 18:24

      So i used the frameless window hint to remove the titlebar and implemented a custom menubar which lets me move, maximise, minimize and close the window.
      However when i move very fast the window sometimes gets stuck somehow, but i couldn't figure out why and what im doing wrong?
      Also i couldn't figure out how to make the window dock/snap when being dragged to the sides or far top or bottom:

      3668da15-e6af-4017-ab29-35256b7172d3-image.png

      #include "CustomMenubar.h"
      #include <QApplication>
      #include <QMouseEvent>
      
      CustomMenuBar::CustomMenuBar(QWidget *parent)
          : QMenuBar{parent}
      {
      
      
      }
      
      
      
      void CustomMenuBar::mousePressEvent(QMouseEvent *event)
      {
          QAction *action = actionAt(event->pos());
          if (action && action->menu()) {
              // If a MenuItem with a submenu is clicked, we won't start moving the window
              clickedOnMenuItem = true;
              isMoving = false;
              QMenuBar::mousePressEvent(event);
          } else {
              QMenuBar::mousePressEvent(event);
              pressPos = event->globalPosition();
              isMoving = true;
              event->accept();
          }
      
      
      }
      
      void CustomMenuBar::mouseReleaseEvent(QMouseEvent *event)
      {
          QAction *action = actionAt(event->pos());
          if (action && action->menu()) {
      
              QMenuBar::mouseReleaseEvent(event);
              return;
          }
      
          // If the mouse was being used for dragging, stop moving
          if (isMoving) {
              isMoving = false;
              event->accept();
          }
      }
      
      void CustomMenuBar::mouseMoveEvent(QMouseEvent *event)
      {
          QAction *action = actionAt(event->pos());
          if (action && action->menu()) {
      
              QMenuBar::mouseMoveEvent(event);
              return;
          }
      
      
          // If the mouse was pressed and is moving, move the window
          if (isMoving) {
              QMenuBar::mouseMoveEvent(event);
              QPointF diff = event->globalPosition() - pressPos;
              QWidget *window = QApplication::topLevelAt(pressPos.toPoint());
              if (window && window->isWindow()) {
                  window->move(window->pos() + diff.toPoint());
                  pressPos = event->globalPosition();
                  event->accept();
              }
          }
      }
      
      

      Thanks in advance. :) Have a nice evening.

      P Offline
      P Offline
      Pl45m4
      wrote on 22 Mar 2024, 19:32 last edited by
      #2

      @StudentScripter

      But we are already dealing with a QDockWidget, aren't we?
      AFAIK you can only dock QDockWidgets and its subclasses.

      Has your QMainWindow and/or window all dockAreas enabled?

      • https://doc.qt.io/qt-6/qt.html#DockWidgetArea-enum

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      S 1 Reply Last reply 23 Mar 2024, 08:03
      0
      • P Pl45m4
        22 Mar 2024, 19:32

        @StudentScripter

        But we are already dealing with a QDockWidget, aren't we?
        AFAIK you can only dock QDockWidgets and its subclasses.

        Has your QMainWindow and/or window all dockAreas enabled?

        • https://doc.qt.io/qt-6/qt.html#DockWidgetArea-enum
        S Offline
        S Offline
        StudentScripter
        wrote on 23 Mar 2024, 08:03 last edited by StudentScripter
        #3

        @Pl45m4 No i don't mean a qdockwidget i mean the docking behaviour of window. Like normally you can append a window to a side of the screen like splitscreen, so another application can fit on the other side.

        When calling frameless window this ability gets lost so i was looking for a way to manually recover it. Any idea?
        4255cc8c-865f-4d41-8905-4910b7e108fe-image.png

        P 1 Reply Last reply 23 Mar 2024, 13:30
        0
        • S StudentScripter
          23 Mar 2024, 08:03

          @Pl45m4 No i don't mean a qdockwidget i mean the docking behaviour of window. Like normally you can append a window to a side of the screen like splitscreen, so another application can fit on the other side.

          When calling frameless window this ability gets lost so i was looking for a way to manually recover it. Any idea?
          4255cc8c-865f-4d41-8905-4910b7e108fe-image.png

          P Offline
          P Offline
          Pl45m4
          wrote on 23 Mar 2024, 13:30 last edited by Pl45m4
          #4

          @StudentScripter

          This?

          WindowSplit.png

          It's called Split Window / Window Tile or something like that and is a Window Manager functionality (in your case, X on Windows 7/10).
          Because frameless Qt toplevel Widgets don't request a Window decoration, I think X does not recognize that you want to snap/split it anyway.
          This is probably not doable with Qt only and platform/Window Manager dependent, maybe you need some extension. Haven't found one though.

          Edit:
          If you are fine with not using the native functionality, you could try to implement something like that using your Qt widget... when enabled, calculate the requested area (1/2, 1/4...) of your screen size and geometry and resize + move your widget accordingly.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0

          1/4

          22 Mar 2024, 18:24

          • Login

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