Qt Borderless Form change size perfect implementation

Source: Internet
Author: User

Recently, do the project to use the borderless form, the egg is painful is the borderless form size changes to like the right border, up and down around, and to smooth.

Online also find some code, found incredibly also to connect to Windows event, this is obviously unreasonable, later own new demo, wrote for one hours, too many problems, throw aside first.

Today needs to improve the UI interface, no way, re-organized the next idea, did not expect to make it. Below to share the implementation of the process, perhaps a rookie column, master do not spray ~

1. First of all, we need to understand the concept of nine Gongge

A form can be divided into upper, lower, left, right, top left, left, right, bottom, right, middle, except the middle part, the other need to write program processing.

The padding is defined in the program as 2, and the enumeration type is defined at the same time.

#define PADDING 2
Enum Direction {up=0, down=1, left, right, Lefttop, Leftbottom, Rightbottom, Righttop, NONE};

2. Define a method with the parameter as the current global position of the mouse.

void Dialog::region (const qpoint &cursorglobalpoint) {
Gets the position area of the form on the screen, TL is TopLeft point, RB is Rightbottom point qrect rect = This->rect (); Qpoint tl = Maptoglobal (Rect.topleft ()); Qpoint RB = Maptoglobal (Rect.bottomright ());
int x = Cursorglobalpoint.x (); int y = Cursorglobalpoint.y (); if (tl.x () + PADDING >= x && tl.x () <= x && tl.y () + PADDING >= y && tl.y () <= y) { upper left corner dir = lefttop; This->setcursor (Qcursor (qt::sizefdiagcursor)); Set Mouse Shape} else if (x >= rb.x ()-PADDING && x <= rb.x () && y >= rb.y ()-PADDING && y <= rb.y ()) {//lower right dir = rightbottom; This->setcursor (Qcursor (qt::sizefdiagcursor)); } else if (x <= tl.x () + PADDING && x >= tl.x () && y >= rb.y ()-PADDING && y <= rb.y () ) {//lower left dir = leftbottom; This->setcursor (Qcursor (qt::sizebdiagcursor)); } else if (x <= rb.x () && x >= rb.x ()-PADDING && y >= tl.y () && y <= tl.y () + PADDING ) {//upper right corner dir = righttop; This->setcursor (Qcursor (qt::sizebdiagcursor)); } else if (x <= tl.x () + PAdding && x >= tl.x ()) {//dir = left; This->setcursor (Qcursor (qt::sizehorcursor)); } else if (x <= rb.x () && x >= rb.x ()-PADDING) {//on the right dir = n; This->setcursor (Qcursor (qt::sizehorcursor)); }else if (y >= tl.y () && y <= tl.y () + PADDING) {//upper dir = up; This->setcursor (Qcursor (qt::sizevercursor)); } else if (y <= rb.y () && y >= rb.y ()-PADDING) {//Bottom dir = down; This->setcursor (Qcursor (qt::sizevercursor)); }else {//default DIR = NONE; This->setcursor (Qcursor (qt::arrowcursor)); }}

3. Define several private member variables in the dialog class

BOOL Isleftpressdown;  Determine whether the left button is pressed qpoint dragposition;   Points to remember when the window moves and drags
Direction dir;
When the window size changes, the record changes direction

Write a dialog constructor, initialize several variables, and do some other work.

Isleftpressdown = False;this->dir = None;this->setminimumheight (+); This->setminimumwidth (;this->) Setwindowflags (qt::framelesswindowhint| Qt::windowsystemmenuhint);                    Set to No Border dialog box this->setmousetracking (TRUE); Track Mouse This->setstylesheet ("Qdialog{background:url (:/bg_main.png)}"); Set style background color, optional

4. Then there are several important overloaded events to be implemented.

void Mousereleaseevent (qmouseevent *event); void Mousemoveevent (qmouseevent *event); void Mousepressevent (QMouseEvent *event);

The implementation process is as follows:

void Dialog::mousereleaseevent (Qmouseevent *event) {if (Event->button () = = Qt::leftbutton) {Isleftpressdown =        False            if (dir! = NONE) {this->releasemouse ();        This->setcursor (Qcursor (qt::arrowcursor)); }}}void dialog::mousepressevent (qmouseevent *event) {switch (Event->button ()) {case Qt::leftbutton:is        Leftpressdown = true;        if (dir! = NONE) {this->mousegrabber ();        } else {dragposition = Event->globalpos ()-This->framegeometry (). TopLeft ();    } break;        Case Qt::rightbutton:this->close ();    Break    Default:qdialog::mousepressevent (event);    }}void dialog::mousemoveevent (qmouseevent *event) {Qpoint glopoint = Event->globalpos ();    Qrect rect = This->rect ();    Qpoint tl = Maptoglobal (Rect.topleft ());    Qpoint RB = Maptoglobal (Rect.bottomright ());    if (!isleftpressdown) {this->region (glopoint);     } else {   if (dir! = NONE) {qrect rmove (TL, RB);                    Switch (dir) {case left:if (rb.x ()-glopoint.x () <= this->minimumwidth ())                Rmove.setx (tl.x ());                Else Rmove.setx (Glopoint.x ());            Break                Case RIGHT:rMove.setWidth (Glopoint.x ()-tl.x ());            Break                Case Up:if (RB.Y ()-Glopoint.y () <= this->minimumheight ()) Rmove.sety (Tl.y ());                Else Rmove.sety (Glopoint.y ());            Break                Case DOWN:rMove.setHeight (Glopoint.y ()-tl.y ());            Break Case Lefttop:if (rb.x ()-glopoint.x () <= this->minimumwidth ()) Rmove.setx (tl.x ())                ;                Else Rmove.setx (Glopoint.x ());                if (Rb.y ()-Glopoint.y () <= this->minimumheight ())    Rmove.sety (Tl.y ());                Else Rmove.sety (Glopoint.y ());            Break                Case RIGHTTOP:rMove.setWidth (Glopoint.x ()-tl.x ());                Rmove.sety (Glopoint.y ());            Break                Case LEFTBOTTOM:rMove.setX (Glopoint.x ());                Rmove.setheight (Glopoint.y ()-tl.y ());            Break                Case RIGHTBOTTOM:rMove.setWidth (Glopoint.x ()-tl.x ());                Rmove.setheight (Glopoint.y ()-tl.y ());            Break            Default:break;        } this->setgeometry (Rmove);            } else {Move (Event->globalpos ()-dragposition);        Event->accept (); }} qdialog::mousemoveevent (event);}

So far, a borderless form is dragging and resizing, and the Run and debug it.

Summing up, this algorithm is not really complicated, just see a few points can not think:

1) Form the rectangular area to be converted to the area on the screen, I take the way is to take TopLeft and rightbottom two points to determine the area.

2) Move the mouse to the global coordinates.

3) The region function to judge the coordinate interval, and then change the mouse shape, this piece is very easy to error, if you write it all at once, then I really admire.

This tangled online have no ready-made code problems, and finally in today's successful solution. To run, please download the example code: Sizablenoframe.rar

Reprint please indicate source OH: http://www.cnblogs.com/xufeiyang/p/3313104.html

Http://www.cnblogs.com/xufeiyang/p/3313104.html

Download:

http://download.csdn.net/detail/qq_33266987/9497160

Http://www.qtcn.org/bbs/read-htm-tid-55986.html

Qt Borderless Form change size perfect implementation

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.