1.QT Custom title bar, drag the title bar to move the window (only drag the title, other positions cannot be dragged)
Method One:
Reprint: http://blog.sina.com.cn/s/blog_4ba5b45e0102e83h.html
The. h file
1 //re-implement the drag operation yourself2 protected:3 4 voidMousemoveevent (Qmouseevent *Event );5 6 voidMousepressevent (Qmouseevent *Event );7 8 voidMousereleaseevent (Qmouseevent *);9 Ten Private: One //re-implement the drag operation yourself A Qpoint mouseposition; - BOOLismousepressed;
The. cpp file
//length of title barConst Static intpos_min_x =0;Const Static intpos_max_x = -- +;Const Static intPos_min_y =0;Const Static intPos_max_y = -;//Self-implemented window drag operationsvoidMainwindow::mousepressevent (Qmouseevent *Event) {mouseposition=Event-pos ();//handle only mouse events that are in the range of the title barif(Mouseposition.x () <=pos_min_x)return;if(Mouseposition.x () >=pos_max_x)return;if(MOUSEPOSITION.Y () <=pos_min_y)return;if(MOUSEPOSITION.Y () >=pos_max_y)return; ismousepressed=true;}voidMainwindow::mousemoveevent (Qmouseevent *Event){if(ismousepressed==true) {Qpoint movepot=Event->globalpos ()-Mouseposition;move (Movepot);}}voidMainwindow::mousereleaseevent (Qmouseevent *Event) {ismousepressed=false;}
Method Two: (Can drag the window any position)
Reprint: http://blog.sina.com.cn/s/blog_a6fb6cc90101au8r.html
Custom interface steps:
1. Set the title bar to hide
Set the title bar to hide and set at the top level
Setwindowflags (Qt::framelesswindowhint | Qt::windowstaysontophint);
Get Mouse tracking effects
Setmousetracking (TRUE);
Note: Qt::windowstaysontophint This is very important, if there is no such sentence even if the custom interface is successful, the interface can be dragged, but there is also a problem, that is, the interface can be dragged under the taskbar!
2, declaring variables and mouse events qpoint move_point;//Distance to move BOOLmouse_press;//mouse button down//Mouse Down Events voidMousepressevent (Qmouseevent *Event); //Mouse Release Events voidMousereleaseevent (Qmouseevent *Event); //Mouse Move Event voidMousemoveevent (Qmouseevent *Event);
3, defining mouse eventsvoidLogindialog::mousepressevent (Qmouseevent *Event) { if(Event->button () = =Qt::leftbutton) {mouse_press=true; //mouse position relative to the form (or use Event->globalpos ()-This->pos ())Move_point =Event-pos ();; } } voidLogindialog::mousemoveevent (Qmouseevent *Event) { //if the left mouse button is pressed if(mouse_press) {//the position of the mouse relative to the screenQpoint Move_pos =Event-Globalpos (); //move the main form location This->move (Move_pos-move_point); } } voidLogindialog::mousereleaseevent (Qmouseevent *Event) { //set the mouse to not be pressedMouse_press =false; }
Method Three:
Drag and drop the custom window to move anywhere
Reprint: http://twyok.blog.163.com/blog/static/81229303201321545850433/
The. h file
protected : void mousepressevent (qmouseevent *)void mousemoveevent (qmouseevent* ); Private : Qpoint last; // Save the mouse-pressed position
The. cpp file
void xxxdialog::mousepressevent (qmouseevent *e) { = e->void xxxdialog:: Mousemoveevent (qmouseevent *e) { if(e->buttons () = = Qt::leftbutton ) {= E->Globalpos (); // calculates the offset from the original position Move (upleft); // update the original location to the latest location You can now use the mouse to manually window to any location.
QT Notes Custom window drag and drop move