Qt setting rounded corners of window borders (two methods using QSS and PaintEvent)

Source: Internet
Author: User


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)


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

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.