We all know that the interface of QT5 is not so beautiful, and every software we find their borders are custom, so I decided to write a blog post, perhaps there are many articles written by Daniel, but I still want to write a record of their own learning QT course
First, we have to write the borderless code in the corresponding constructor (for example, the blogger wants Mywidget to become borderless)
Setwindowflags (Qt::framelesswindowhint | Qt::windowstaysontophint);
Set the border state, after no border, you must write a mouse event to drag it
Setmousetracking (TRUE); Turn on mouse tracking events
We're going to have three functions, press,release, and move, and a bool press; Determine if the mouse is pressed, as well as the Qpoint Start_pos; record the starting position, Qpoint End_pos, record the end position, and their difference is how far we want to move. For example, the starting position is (0,0) the end position is (200,300), then our borderless interface moves (200,300) units. The most important thing to do is to initialize the Press=flase, because you didn't press the mouse at first.
void Mywidget::mousepressevent (Qmouseevent *e)
{
Press = TRUE;
Start_pos=e->pos ();
}
void Mywidget::mousemoveevent (Qmouseevent *e)
{
if (press)
{
Qpoint End_pos = E->globalpos ();
In the process of moving, the coordinates are changing, only when the mouse is released, the press becomes false, coordinates are determined, the window starts to move
This->move (End_pos-start_pos);
}
}
void Mywidget::mousereleaseevent (Qmouseevent *)
{
Press=false; Used to end the coordinate changes in the mousemoveevent and to determine the end coordinates
}
References from Baidu. Belong to Bo Master Original, reproduced please indicate the source
QT 5 Little practice Create borderless interface