QT has two ways of setting the window border fillet, one is to set the style, and the other is to draw the window in the PaintEvent event. The following describes the effect of using these two methods to achieve the window border rounded corners.
First, using the Setstylesheet method
This->setstylesheet ("qwidget{border-top-left-radius:15px;border-top-right-radius:5px;}"));
The main use of the Border-radius property, with regard to this property, is that the optional style has
Border-top-left-radius set the upper left corner round corner;
Border-top-right-radius set the upper right corner rounded corner;
Border-bottom-left-radius set the bottom left corner round corner;
Border-bottom-right-radius set the lower right corner rounded corner;
Border-radius set four corner fillets;
About the parameters behind Border-radius
(1) One parameter
border-radius:15px
(2) two parameters
border-radius:15px 50px
The first parameter sets the radius of the x-axis direction
The second parameter sets the radius of the y-axis direction
You can see that setting a parameter represents the same value for the X and Y axes, and two parameters are the radius of the x-axis and y-axis respectively. You can set the parameters according to different needs.
Second, draw the window border in the PaintEvent event
Here we need to rewrite the PaintEvent method with the following code:
void paintEvent (QPaintEvent * event)
{
QPainter painter (this);
painter.setRenderHint (QPainter :: Antialiasing); // Anti-aliasing;
painter.setBrush (QBrush (Qt :: red));
painter.setPen (Qt :: transparent);
QRect rect = this-> rect ();
rect.setWidth (rect.width ()-1);
rect.setHeight (rect.height ()-1);
painter.drawRoundedRect (rect, 15, 15);
// You can also use QPainterPath to draw instead of painter.drawRoundedRect (rect, 15, 15);
{
QPainterPath painterPath;
painterPath.addRoundedRect (rect, 15, 15);
p.drawPath (painterPath);
}
QWidget :: paintEvent (event);
}
The effect is as follows:
If you do not write Painter.setrenderhint (qpainter::antialiasing), the fillet will appear jagged, such as.
You can see that the fillet curve has a jagged shape in a careful contrast.
Attention:
(1) When using these two methods, you need to set the properties of the window.
this-> setAttribute (Qt :: WA_TranslucentBackground); // Set the window background transparent
this-> setWindowFlags (Qt :: FramelessWindowHint); // Set borderless window
(2) When the main window cannot load the style, you need to add the following code to the paintEvent event.
{
QStyleOption opt;
opt.init (this);
QPainter p (this);
style ()-> drawPrimitive (QStyle :: PE_Widget, & opt, & p, this);
QWidget :: paintEvent (event);
}
Set the background border of a button
Original Diagram
set no border or background transparency to remove the white box
Set the following style for the button.
{}
Or
{Border:none;}
http://blog.csdn.net/goforwardtostep/article/details/52084538
Qt setting rounded corners of window borders (two methods using QSS and PaintEvent)