QT application programming: set color transparency

1. Environmental introduction

Operating system introduction: win10 64 bit

QT version:  5.12.6

2. Function introduction

When drawing a custom window, other graphic effects are often drawn on the background, and you don't want to overwrite the background color. At this time, you can set the transparency of the drawing color to achieve the effect.

QColor color("#61DED0");
color.setAlpha(100); //其中,参数x为透明度,取值范围为0~255,数值越小越

Three, sample code

/*
工程: DrawTimeLine
日期: 2020-12-25
作者: DS小龙哥
环境: win10 QT5.12.6 MinGW32
功能:窗口绘制事件
*/
void DrawTimeLine::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
	//启动抗锯齿
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
	
	.............................
	
    QColor color("#61DED0");
    color.setAlpha(100); //其中,参数x为透明度,取值范围为0~255,数值越小越透明
    QBrush posBrush(color);
    QRectF posRect(0,20, 100,100);
    painter.setBrush(posBrush); //设置画刷
    painter.setPen(Qt::NoPen);  //不设置画笔,不绘制边界线
    painter.drawRect(posRect);  //绘制矩形
	
	.............................
}

 

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/111661223