How can I move a window around when using Qt::FramelessWindowHint?

You need to reimplement the mouse event handlers for the widgets you would like be able to move your window with. Here is an example using QMenuBar [doc.qt.nokia.com]:

  1. #ifndef CMENUBAR_H
  2. #define CMENUBAR_H
  3.  
  4. #include <QMenuBar>
  5.  
  6. class QPoint;
  7. class QMouseEvent;
  8. class QWidget;
  9.  
  10. class CMenuBar : public QMenuBar
  11. {
  12. public:
  13.     CMenuBar();
  14.     CMenuBar(QWidget *par);
  15.  
  16. protected:
  17.     void mousePressEvent(QMouseEvent *event);
  18.     void mouseMoveEvent(QMouseEvent *event);
  19.     void mouseReleaseEvent(QMouseEvent *event);
  20.  
  21. private:
  22.     QPoint offset;
  23.     bool moving;
  24. };
  25.  
  26. #endif // CMENUBAR_H
  27.  
  28. #include "cmenubar.h"
  29. #include <QPoint>
  30. #include <QMouseEvent>
  31. #include <QDebug>
  32.  
  33. CMenuBar::CMenuBar()
  34. {
  35.     moving = false;
  36. }
  37. CMenuBar::CMenuBar(QWidget *par)
  38. {
  39.     setParent(par);
  40.     moving = false;
  41.     setMouseTracking(false);
  42. }
  43.  
  44. void CMenuBar::mousePressEvent(QMouseEvent *event)
  45. {
  46.     QMenuBar::mousePressEvent(event);
  47.     if((event->button() == Qt::LeftButton) && !actionAt(event->pos())) {
  48.         moving = true;
  49.         offset = event->pos();
  50.     }
  51. }
  52.  
  53. void CMenuBar::mouseMoveEvent(QMouseEvent *event)
  54. {
  55.     QMenuBar::mouseMoveEvent(event);
  56.     if(moving)
  57.         parentWidget()->move(event->globalPos() - offset);
  58. }
  59.  
  60. void CMenuBar::mouseReleaseEvent(QMouseEvent *event)
  61. {
  62.     QMenuBar::mouseReleaseEvent(event);
  63.     if(event->button() == Qt::LeftButton) {
  64.         moving = false;
  65.     }
  66. }

No comments

Write a comment

Sorry, you must be logged in to post a comment.