"Qt" Move borderless form

Source: Internet
Author: User

 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
  1. void Editdialog::mousepressevent (Qmouseevent *event)
  2. {
  3. if (event->button () = = Qt::leftbutton) {
  4. M_dragposition = Event->globalpos ()- this->pos ();
  5. Event->accept ();
  6. }
  7. }
  8. void Editdialog::mousemoveevent (Qmouseevent *event)
  9. {
  10. if (event->buttons () && Qt::leftbutton) {
  11. Move (Event->globalpos ()-m_dragposition);
  12. Event->accept ();
  13. }
  14. }

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
  1. void Editdialog::mousepressevent (Qmouseevent *event)
  2. {
  3. if (event->button () = = Qt::leftbutton) {
  4. M_drag = true;
  5. M_dragposition = Event->globalpos ()- this->pos ();
  6. Event->accept ();
  7. }
  8. }
  9. void Editdialog::mousemoveevent (Qmouseevent *event)
  10. {
  11. if (M_drag && (event->buttons () && Qt::leftbutton)) {
  12. Move (Event->globalpos ()-m_dragposition);
  13. Event->accept ();
  14. }
  15. }
  16. void Editdialog::mousereleaseevent (Qmouseevent *)
  17. {
  18. M_drag = false;
  19. }


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
    1. void Mainwindow::mousepressevent (Qmouseevent *event)
    2. {
    3. if (releasecapture ())
    4. SendMessage (HWND (This->winid ()), Wm_syscommand, Sc_move + htcaption, 0);
    5. Event->ignore ();
    6. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.