少毛「C / C ++チュートリアルシリーズ」Qtインターフェイス開発から転載https://chuanke.baidu.com/v4509752-209060-1284517.html
唯一の小さなストリップはトップをドラッグすることができます
1 プライベート: 2 仮想 ボイド MouseMoveEvent(QMouseEvent *。イベント); 3。 仮想 ボイド mousePressEvent(QMouseEvent * イベント); 4。 仮想 ボイド mouseReleaseEvent(QMouseEvent * イベント); 5。 6。 BOOLの m_dragging; // ドラッグされた場合 7。 のQPoint m_startPosition; // 開始前にマウスの位置をドラッグ 8。 のQPoint m_framePosition; // 元の位置を形成する 9。 10 11。 // ui.setupUi(この)書き込まれ;下に 12 = m_dragging falseに、 13は 14 // タイトルバー(またフレーム)なしで 15 setWindowFlags(Qtの::ウィンドウ| Qtの:: FramelessWindowHint)は、 16 17 18れている 。19 ボイド TestAandB :: mousePressEvent(QMouseEvent * イベント) 20は、 { 21れる // 左側のみに応答し 22である IF(イベント - >ボタン()== Qtの:: leftButton) 23である { 24 QRect titleRect = RECT(); 25 titleRect.setBottom(titleRect.top()+ 80 ); 26である 27 IF(titleRect.contains(イベント - > POS())) 28 { 29 m_dragging = 真。 30 m_startPosition = イベント - > globalPos(); 31 m_framePosition = frameGeometry()TOPLEFT()。 32 } 33 } 34 35 QWidgetの:: mousePressEvent(イベント)。 36 } 37 38 空隙 TestAandB :: mouseMoveEvent(QMouseEvent * イベント) 39 { 40 // 只响应左键 41は IF(イベント - >ボタン()&Qtの:: leftButton) 42である { 43である IF (m_dragging) 44である { 45 // デルタに対しては、オフセット 46がある のQPointデルタ= イベント - > globalPos() - m_startPosition; 47 48 // 新しい位置:元の位置+形オフセット 49 移動(m_framePosition + デルタ); 50 } 51である } 52が 53である QWidgetの:: mouseMoveEvent(イベント); 54である } 55 56である ボイドTestAandB :: mouseReleaseEvent(QMouseEvent * イベント) 57 { 58 m_dragging = 偽。 59 のQWidget :: mouseReleaseEvent(イベント)。 60 }
すべての缶ドラッグ
1 空隙 QTXiDaoPanJieMian1 :: mousePressEvent(QMouseEvent * イベント) 2 { 3 // 只响应左键 4 であれば(イベント - >ボタン()== Qtの:: LeftButton) 5 { 6 m_dragging = 真。 7 m_startPosition = イベント - > globalPos(); 8 m_framePosition = 。frameGeometry()TOPLEFT(); 9 } 10 QWidgetの:: mousePressEvent(イベント)。 11 } 12 13 空隙。:: MouseMoveEvent QTXiDaoPanJieMian1(QMouseEvent * イベント) 14 { 15 // 応答だけ左 16 IF(イベント - >ボタン()&Qtの:: leftButtonを) 。17 { 18で IF (m_dragging) 19 { 20れる // デルタ相対オフセット、 21である のQPointデルタ= イベント - > globalPos() - m_startPosition; 22である 23れる // 新しい位置:オフセットフォーム元の位置+ 24 移動(m_framePosition + デルタ); 25 } 26である } 27 QWidgetの:: mouseMoveEvent(イベント)。 28 } 29 30 空隙 QTXiDaoPanJieMian1 :: mouseReleaseEvent(QMouseEvent * イベント) 31 { 32 // 结束ドラッグ 33 m_dragging = 偽。 34 QWidgetの:: mouseReleaseEvent(イベント)。 35 }