QT can drag the custom title

MainWindow.cpp
1
#include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) : 5 QMainWindow(parent), 6 ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 10 setWindowFlags(Qt::FramelessWindowHint); 11 m_bMouseLftBtnPressed = false; 12 } 13 14 MainWindow::~MainWindow() 15 { 16 Delete UI; . 17 } 18 is . 19 / * --------------------------------------- ------------------------------------ 20 function name: mouseMoveEvent 21 function: move the mouse event function, in realization of a window inside the function drag function 22 only to the window drag the left mouse button pressed in the case of 23 ---------------------- -------------------------------------------------- --- * / 24 void the MainWindow ::. mouseMoveEvent (QMouseEvent * Event ) 25 { 26 is IF ( the this -> isMaximized ()) // if the current is maximized, is not allowed to move 27 return ; 28 29 IF (( Event -> Buttons () :: & leftButton the Qt) && m_bMouseLftBtnPressed) // is depressed the left mouse button 30 { 31 is QPoint ptTemp = Event -> globalPos (); // current global position of the mouse 32 ptTemp = ptTemp - m_ptMouseLatestPos; // calculate a movement variable 33 is ptTemp ptTemp + POS = (); // window original position (pos ()) + mouse movement variable (ptemp) = the final position of the window 34 is move (ptTemp); // move the window to a new position 35 36 m_ptMouseLatestPos = Event -> globalPos (); //Record the current position of the mouse click 37 } 38 } 39 40 41 is / * ---------------------------------- ----------------------------------------- 42 function name: mousePressEvent 43 function: mouse press the button event function within the function to determine whether you can drag the window 44 only press the left mouse button in the title bar area can drag the current window 45 --------------- -------------------------------------------------- ---------- * / 46 is void the MainWindow :: mousePressEvent (QMouseEvent * Event ) 47 { 48 the this -> setFocus (); 49 50 // only by pressing the left mouse button in the title bar area and the current can drag a maximized window is not the case 51 IF(Qt :: leftButton == Event -> the Button () && 0 == (Qt :: WindowMaximized & the this -> windowState ())) 52 { 53 // just press the left mouse button in the title bar area before they can be drag operation 54 is IF (ui-> TitleWidget-> Geometry () the contains (. Event -> POS ())) 55 { 56 is m_ptMouseLatestPos = Event -> globalPos (); // record the position of the mouse click 57 is m_bMouseLftBtnPressed = to true ; // tag is pressed the mouse 58 Event -> the ignore (); 59 } 60 } 61 } 62 is 63 is 64 / * ---------------------------------------- ----------------------------------- 65 function name: mouseReleaseEvent 66 function: the mouse button up event function, within this window function will be canceled drag operation 67 only considered to be the end of the left mouse button and drag the case raised 68 --------------------- -------------------------------------------------- ---- * / 69 void the MainWindow :: mouseReleaseEvent (QMouseEvent * Event ) 70 { 71 is IF (the Qt leftButton == :: Event -> Button ()) 72 { 73 is m_bMouseLftBtnPressed = false ; 74 } 75 }
MainWindow.h
1
#include <QMainWindow> 2 3 namespace Ui { 4 class MainWindow; 5 } 6 7 class MainWindow : public QMainWindow 8 { 9 Q_OBJECT 10 11 public: 12 explicit MainWindow(QWidget *parent = 0); 13 ~MainWindow(); 14 protected: 15 // 鼠标移动事件函数 16 void mouseMoveEvent(QMouseEvent *event); . 17 18 is // the mouse button event function . 19 void mousePressEvent (QMouseEvent * Event ); 20 is 21 is // mouse button up event function 22 is void mouseReleaseEvent (QMouseEvent * Event ); 23 is 24 Private : 25 Ui :: * the MainWindow ui; 26 QPoint m_ptMouseLatestPos; // mouse position of the last 27 BOOL m_bMouseLftBtnPressed; // identifies whether the current press the left mouse button 28 29 };
needs its own drag a window up
 

 

Guess you like

Origin www.cnblogs.com/that-boy-done/p/11360530.html