QT interface development - (special effects) as a whole can drag a window

Reprinted from Shao hair "C / C ++ tutorial series" Qt interface development https://chuanke.baidu.com/v4509752-209060-1284517.html

Only a small strip can drag the top

. 1  Private :
 2      Virtual  void . MouseMoveEvent (QMouseEvent * Event );
 . 3      Virtual  void mousePressEvent (QMouseEvent * Event );
 . 4      Virtual  void mouseReleaseEvent (QMouseEvent * Event );
 . 5  
. 6      BOOL m_dragging; // if being dragged 
. 7      QPoint m_startPosition; // drag the mouse position before the start 
. 8      QPoint m_framePosition; // form the original position
 . 9  
10  
. 11  // written ui.setupUi (this); below 
12     = m_dragging to false ;
 13 is  
14      // without the title bar (nor frame) 
15      setWindowFlags (the Qt :: the Window | the Qt :: FramelessWindowHint);
 16  
. 17  
18 is  
. 19  void TestAandB :: mousePressEvent (QMouseEvent * Event )
 20 is  {
 21 is      // only in response to the left 
22 is      IF ( Event -> Button () == the Qt :: leftButton)
 23 is      {
 24          QRect titleRect = RECT ();
 25          titleRect.setBottom (titleRect.top () + 80 );
 26 is  
27          IF (titleRect.contains(event->pos()))
28         {
29             m_dragging = true;
30             m_startPosition = event->globalPos();
31             m_framePosition = frameGeometry().topLeft();
32         }
33     }
34 
35     QWidget::mousePressEvent(event);
36 }
37 
38 void TestAandB::mouseMoveEvent(QMouseEvent *event)
39 {
40     // 只响应左键
41 is      IF ( Event -> Buttons () & the Qt :: leftButton)
 42 is      {
 43 is          IF (m_dragging)
 44 is          {
 45              // Delta relative offset, 
46 is              QPoint Delta = Event -> globalPos () - m_startPosition;
 47  
48              // new position: offset form the original position + 
49              Move (m_framePosition + Delta);
 50          }
 51 is      }
 52 is  
53 is      the QWidget ::. mouseMoveEvent ( Event );
 54 is  }
 55  
56 is  void TestAandB::mouseReleaseEvent(QMouseEvent * event)
57 {
58     m_dragging = false;
59     QWidget::mouseReleaseEvent(event);
60 }

All can drag

 1 void QTXiDaoPanJieMian1::mousePressEvent(QMouseEvent *event)
 2 {
 3     // 只响应左键
 4     if (event->button() == Qt::LeftButton)
 5     {
 6         m_dragging = true;
 7         m_startPosition = event->globalPos();
 8         m_framePosition = frameGeometry().topLeft();
 9     }
10     QWidget::mousePressEvent(event);
11 }
12 
13 void::. MouseMoveEvent QTXiDaoPanJieMian1 (QMouseEvent * Event )
 14  {
 15      // respond only left 
16      IF ( Event -> Buttons () & the Qt :: leftButton)
 . 17      {
 18 is          IF (m_dragging)
 . 19          {
 20 is              // Delta relative offset , 
21 is              QPoint Delta = Event -> globalPos () - m_startPosition;
 22 is  
23 is              // new position: offset form the original position + 
24              Move (m_framePosition + Delta);
 25          }
 26 is      }
 27     QWidget::mouseMoveEvent(event);
28 }
29 
30 void QTXiDaoPanJieMian1::mouseReleaseEvent(QMouseEvent * event)
31 {
32     // 结束dragging
33     m_dragging = false;
34     QWidget::mouseReleaseEvent(event);
35 }

Guess you like

Origin www.cnblogs.com/nxopen2018/p/12176486.html