当前位置:网站首页>QT custom window fillets
QT custom window fillets
2022-06-12 10:15:00 【BUG_ C++】
Qt Setting fillet
stay Qt There are two ways to set window fillets in ,
- QSS
- adopt paintEvent Self drawing window
QSS Setting fillet
This method of setting fillets is relatively flexible , But we set the base class window ( Outermost window ) use qss It's not convenient , Will receive follow-up qss Influence , In addition, when the fillet size exceeds the height 1/2 when , The fillet effect will disappear , So when the window changes dynamically , This is not convenient to use
border-radius:8px;// Four corners
border-top-left-radius:8px; top left corner ;
border-top-right-radius:8px; Upper right corner ;
border-bottom-left-radius:8px; The lower left corner ;
border-bottom-right-radius:8px; The lower right corner ;
paintEvent Self drawing window
The most common is to draw four corners at the same time
void CustomRadiusWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(bg_color_);
painter.setPen(Qt::transparent);
painter.drawRoundedRect(rect(), radius_, radius_);
}
But this time , If we want to achieve something like qss Let one or several corners realize rounded corners , It's not going to work , I checked it and found nothing related demo. Here I would like to share with you my ideas for implementation
The core idea is : Draw four fillets , Then add a rectangle with edges and corners on the unwanted fillets to cover
Take the upper left corner for example 
Core code
void CustomRadiusWidget::paintEvent(QPaintEvent *event)
{
// Beautification style , Prevent the set fillet from being too large ( Over height 1/2)
auto half_height = height() / 2;
auto radius = radius_ > half_height ? height() / 2 : radius_;
//auto radius = radius_;
QPainter painter(this);
QPainterPath draw_path;
draw_path.addRoundedRect(rect(), radius, radius);
draw_path.setFillRule(Qt::WindingFill);
if (!left_top_) {
draw_path.addRect(0, 0, width() / 2, height() / 2);
}
if (!left_bottom_) {
draw_path.addRect(0, height() / 2, width() / 2, height() / 2);
}
if (!right_top_) {
draw_path.addRect(width() / 2, 0, width() / 2, height() / 2);
}
if (!right_bottom_) {
draw_path.addRect(width() / 2, height() / 2, width() / 2, height() / 2);
}
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::transparent);
painter.setBrush(bg_color_);
painter.drawPath(draw_path);
}
QPainterPath Populate the rule
- Qt::OddEventFill( Parity fill rule ) Determine whether a point is within the shape , Draw a horizontal line from this point to the position outside the shape , And calculate the number of intersections . If the number of intersections is odd , The point is inside the shape . This mode is the default .
- Qt::WindingFill ( Non zero bending rule ) Determine whether a point is within the shape , Draw a horizontal line from this point to the outside of the shape . Determine whether the direction of the line at each intersection is up or down . The number of windings is determined by summing the directions of each intersection . If the number is not zero , The point is inside the shape . This filling pattern can also be considered as the intersection of closed shapes in most cases .

By the above code , You can control which fillet is displayed at will
Complete code
//===================================.h==========================================//
#pragma once
#include <QWidget>
class CustomRadiusWidget : public QWidget
{
Q_OBJECT
public:
CustomRadiusWidget(QWidget *parent=nullptr);
~CustomRadiusWidget();
void SetRadius(int radius);
void SetRadiusEnable(bool left_top, bool left_bottom, bool right_left, bool right_bottom);
protected:
virtual void paintEvent(QPaintEvent* event)override;
private:
int radius_ = {
0};
QColor bg_color_ = {
106 ,90 ,205};
bool left_top_=false;
bool left_bottom_ = false;
bool right_top_ = false;
bool right_bottom_ = false;
};
//===================================.cpp==========================================//
#include "CustomRadiusWidget.h"
#include <QPainter>
#include <QPainterPath>
CustomRadiusWidget::CustomRadiusWidget(QWidget *parent) : QWidget(parent){
setWindowFlag(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground, true);
}
CustomRadiusWidget::~CustomRadiusWidget() {
}
void CustomRadiusWidget::SetRadius(int radius){
radius_ = radius;
}
void CustomRadiusWidget::SetRadiusEnable(bool left_top, bool left_bottom, bool right_top, bool right_bottom){
left_top_ = left_top;
left_bottom_ = left_bottom;
right_top_ = right_top;
right_bottom_ = right_bottom;
}
void CustomRadiusWidget::paintEvent(QPaintEvent *event)
{
auto half_height = height() / 2;
auto radius = radius_ > half_height ? height() / 2 : radius_;
QPainter painter(this);
QPainterPath draw_path;
draw_path.addRoundedRect(rect(), radius, radius);
draw_path.setFillRule(Qt::WindingFill);
if (!left_top_) {
draw_path.addRect(0, 0, width() / 2, height() / 2);
}
if (!left_bottom_) {
draw_path.addRect(0, height() / 2, width() / 2, height() / 2);
}
if (!right_top_) {
draw_path.addRect(width() / 2, 0, width() / 2, height() / 2);
}
if (!right_bottom_) {
draw_path.addRect(width() / 2, height() / 2, width() / 2, height() / 2);
}
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::transparent);
painter.setBrush(bg_color_);
painter.drawPath(draw_path);
}
边栏推荐
- MySQL optimized slow log query
- np. Meshgrid() function and coordinate position generation in 3D space and numpy Introduction to repeat() function
- June training (day 12) - linked list
- [CEGUI] font resource loading process
- 【云原生 | Kubernetes篇】Kubernetes 网络策略(NetworkPolicy)
- 学生管理系统
- [cloud native | kubernetes] kubernetes networkpolicy
- Explanation of the principle of MySQL's leftmost matching principle
- Circuitbreaker fuse of resilience4j - circuitbreakerconfig configuration
- 2021-03-26
猜你喜欢

一文读懂Dfinity生态中的首个NFT平台:IMPOSSIBLE THINGS

UE4_ Explore the method of creating background scenes with ready-made resources

Circuitbreaker fuse of resilience4j -- Measurement of circuitbreakermetrics index

First NFT platform in dfinity Ecology: impossible thoughts

CentOS 7 installing MySQL 8

1268_FreeRTOS任务上下文切换的实现

Implementation of fruit mall wholesale platform based on SSM
![[DDS] ddsi-rtps specification](/img/fe/16b835e3e4a8ff71ab3dbc4b9c4d2a.jpg)
[DDS] ddsi-rtps specification

Explication du principe d'appariement le plus à gauche de MySQL

3. Abstract Factory
随机推荐
Halcon combined with C # to detect surface defects -- affine transformation (III)
Auto. JS learning note 10: instantiate a custom object and use json The stringify() method causes an error (resolved)
1268_FreeRTOS任务上下文切换的实现
奇葩错误 -- 轮廓检测检测到边框、膨胀腐蚀开闭运算效果颠倒
[CEGUI] window environment compilation
markdown_图片并排的方案
Auto. JS debugging: use the network mode of lightning simulator for debugging
原始套接字使用
[path of system analyst] Chapter 18 security analysis and design of double disk system
[Wayland] Wayland agreement description
[Wayland] Weston multi screen display
SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
学生管理系统
JVM (IV) Class file structure (complete parsing of bytecode attached)
传输层协议 ——— TCP协议
[DDS] dds-rpc implementation based on opendds
总有一根阴线(上影线)会阻止多军前进的脚步,总有一个阳线(下影线)会阻挡空军肆虐的轰炸
IoT简介
2021-09-15
CLAHE in opencv for 16 bit image enhancement display