一、前言 在音频开发中,窗体多半为半透明、圆角窗体,如下为Qt5。5VS2013实现半透明方法总结。 二、半透明方法设置 1、窗体及子控件都设置为半透明 1)setWindowOpacity(0。8);参数范围为01。0,通过QSlider控件做成透明度控制条 2)无边框设置setWindowFlags(Qt::FramelessWindowHint); 3)窗体圆角设置setAttribute(Qt::WATranslucentBackground);通过paintEvent绘制窗体背景色与圆角voidWidget::paintEvent(QPaintEventevent){QPainterpainter(this);painter。setRenderHint(QPainter::Antialiasing);反锯齿;painter。setBrush(QBrush(QColor(616F76)));窗体背景色painter。setPen(Qt::transparent);QRectrectthisrect();rect为绘制大小rect。setWidth(rect。width()1);rect。setHeight(rect。height()1);painter。drawRoundedRect(rect,15,15);15为圆角角度也可用QPainterPath绘制代替painter。drawRoundedRect(rect,15,15);QPainterPathpainterPath;painterPath。addRoundedRect(rect,15,15);15为圆角角度painter。drawPath(painterPath);QWidget::paintEvent(event);} 2、通过图片贴图,设置局部透明 1)窗体设置setAttribute(Qt::WATranslucentBackground);背景半透明属性设置setWindowFlags(Qt::FramelessWindowHint);无边框窗体设置 2)采用样式加载图片uimBgWidgetsetStyleSheet(backgroundimage:url(:imagesbg。png););注意:mBgWidget为窗体对象的子窗体,不能直接设置QWidget 3)效果图如下(录制gif时刷新有点延时) 3、通过paintEvent重绘背景色透明度 1)窗体属性设置setAttribute(Qt::WATranslucentBackground);背景半透明属性设置setWindowFlags(Qt::FramelessWindowHint);无边框窗体设置mBgColorQColor(616F76);默认背景色mBgColor。setAlphaF(0。8);thissetContextMenuPolicy(Qt::CustomContextMenu);connect(this,SIGNAL(customContextMenuRequested(constQPoint)),this,SLOT(showContextMenuSlot(constQPoint)));右击出现菜单 2)右击出现菜单voidWidget::showContextMenuSlot(constQPointpos){QActionactNULL;if(NULLmMenu){mMenunewQMenu();菜单mActions。clear();记录所有ActionactmMenuaddAction(1。0,this,SLOT(funcSlot()));mActionsact;actsetCheckable(true);actmMenuaddAction(0。8,this,SLOT(funcSlot()));mActionsact;actsetCheckable(true);设置可选中actsetChecked(true);设置被选中actmMenuaddAction(0。5,this,SLOT(funcSlot()));mActionsact;actsetCheckable(true);actmMenuaddAction(0。3,this,SLOT(funcSlot()));mActionsact;actsetCheckable(true);actmMenuaddAction(0。1,this,SLOT(funcSlot()));mActionsact;actsetCheckable(true);}mMenuexec(mapToGlobal(pos));弹出菜单}3)选择菜单Action,修改背景颜色透明度voidWidget::funcSlot(){QActionactqobjectcastQAction(sender());获取选中的Actionif(act){doublealphaacttext()。toDouble();mBgColor。setAlphaF(alpha);背景色透明度修改foreach(QActionaction,mActions)去除其余选中,互斥{if(act!action)actionsetChecked(false);}thisupdate();刷新界面}} 4)通过paintEvent重绘背景色voidWidget::paintEvent(QPaintEventevent){QPainterpainter(this);painter。setRenderHint(QPainter::Antialiasing);反锯齿;painter。setBrush(QBrush(mBgColor));修改后的背景色painter。setPen(Qt::transparent);QRectrectthisrect();rect为绘制窗体大小rect。setWidth(rect。width()1);rect。setHeight(rect。height()1);painter。drawRoundedRect(rect,15,15);15为圆角角度也可用QPainterPath绘制代替painter。drawRoundedRect(rect,15,15);QPainterPathpainterPath;painterPath。addRoundedRect(rect,15,15);painter。drawPath(painterPath);QWidget::paintEvent(event);} 5)效果如下(录频有点重影) 4、通过paintEvent采用Clear模式绘图,实现局部透明 1)窗体属性设置mMargin60;各个绘制图形与边框的距离mBgColorQColor(00BFFF);窗体背景色installEventFilter(this);事件过滤器,用于鼠标按下后界面移动setWindowFlags(Qt::FramelessWindowHint);无边框窗体设置setAttribute(Qt::WATranslucentBackground);背景半透明属性设置 2)画笔属性设置voidWidget::paintEvent(QPaintEvent){QPainterpainter(this);painter。setPen(Qt::NoPen);painter。setBrush(mBgColor);painter。drawRoundedRect(thisrect(),15,15);设置整体窗体圆角为15painter。setCompositionMode(QPainter::CompositionModeClear);设置Clear绘图模式绘制三角形drawTriangle(painter);绘制圆drawCircular(painter);绘制矩形drawRectangle(painter);} 3)绘制三角形voidWidget::drawTriangle(QPainterpainter){QPainterPathpath;intwidththiswidth()2;intheightthisheight()2;顶点inttopXwidth2;inttopYmMargin;左下顶点intleftXmMargin;intleftYheightmMargin;右下顶点intrightXwidthmMargin;intrightYheightmMargin;path。moveTo(topX,topY);起点path。lineTo(leftX,leftY);画线段1path。lineTo(rightX,rightY);画线段2path。lineTo(topX,topY);画线段3painterfillPath(path,QBrush(mBgColor));绘制三角形} 4)绘制圆voidWidget::drawCircular(QPainterpainter){intwidththiswidth()2;intheightthisheight()2;intxwidthwidth2;X向坐标intyheight2;Y向坐标intrwidth2mMargin;第一个参数为中心点,r为x向、y向长度(不一致时可绘制椭圆)painterdrawEllipse(QPoint(x,y),r,r);} 5)绘制矩形voidWidget::drawRectangle(QPainterpainter){intwidththiswidth()2;intheightthisheight()2;intrectWidthwidth2mMargin;矩形宽度intrectHeightheight2mMargin;矩形高度intrectXwidthrectWidth2;矩形X向长度intrectYheightmMargin;矩形Y向长度painterdrawRect(QRect(rectX,rectY,rectWidth,rectHeight));} 6)运行效果