Category: Qt2013-05-08 22:55 3027 People read Comments (7) favorite reports
Mobile borderless form of the code on the Internet a lot, the principle is the same, but there is a problem, I just fix it here
The code on the web only implements two events
[CPP]View Plaincopy
- void Editdialog::mousepressevent (Qmouseevent *event)
- {
- if (event->button () = = Qt::leftbutton) {
- M_dragposition = Event->globalpos ()- this->pos ();
- Event->accept ();
- }
- }
- void Editdialog::mousemoveevent (Qmouseevent *event)
- {
- if (event->buttons () && Qt::leftbutton) {
- Move (Event->globalpos ()-m_dragposition);
- Event->accept ();
- }
- }
However, there is a problem when the mouse clicks on a class that implements the Mousepressevent (for example, Qpushbutton) and the event is prioritized by that class.
The event is not passed to the mousepressevent of the form. Continue, when moving the mouse outside this button (assuming that the point is on the Qpushbutton) will trigger the form's mousemoveevent
resulting in an error in calculating the coordinates, you will see the form flash a bit, change position, the mouse does not stop at the front button pressed.
The solution is also very simple, is to declare a bool variable to judge, and implement Mousereleaseevent can
[CPP]View Plaincopy
- void Editdialog::mousepressevent (Qmouseevent *event)
- {
- if (event->button () = = Qt::leftbutton) {
- M_drag = true;
- M_dragposition = Event->globalpos ()- this->pos ();
- Event->accept ();
- }
- }
- void Editdialog::mousemoveevent (Qmouseevent *event)
- {
- if (M_drag && (event->buttons () && Qt::leftbutton)) {
- Move (Event->globalpos ()-m_dragposition);
- Event->accept ();
- }
- }
- void Editdialog::mousereleaseevent (Qmouseevent *)
- {
- M_drag = false;
- }
This completes the drag of the borderless form. However, this is not efficient, because the mouse every move will trigger the event, calculate the position, move the window, redraw the window ...
When there are qwebview parts on the form, especially if there is a picture in the page, Flash, you will find that using the above scheme to move the form will be very fluid.
If you don't consider cross-platform and only for the Windows platform, then I recommend using the standard method under Windows to simulate the title bar move message, simple and efficient
[CPP]View Plaincopy
- void Mainwindow::mousepressevent (Qmouseevent *event)
- {
- if (releasecapture ())
- SendMessage (HWND (This->winid ()), Wm_syscommand, Sc_move + htcaption, 0);
- Event->ignore ();
- }
This avoids the inefficiency of the first method by dragging the form only when the mouse is released and the form moves past.
"Qt" Move borderless form