Qt不规则异形窗体实现

采用主窗体透明方式实现

具体实现思路:
首先去掉窗体的标题头,设置主窗体为透明属性,然后需要套一个窗体来显示异形图片即可.
关键代码

	this->setWindowFlags(Qt::FramelessWindowHint | this->windowFlags());//去掉标题头
	this->setAttribute(Qt::WA_TranslucentBackground);//主窗体透明
	_layoutMain = new QVBoxLayout;
	_layoutMain->setContentsMargins(0, 0, 0, 0);//背景完全贴合
	_widgetBackground = new QWidget;//另外套一层窗体
	_widgetBackground->setObjectName("widgetBackground");
	_layoutMain->addWidget(_widgetBackground);
    setLayout(_layoutMain);
    readStyleSheetFile("skin/style.css");//在css文件中让 _widgetBackground 来显示异形背景图片

采用 setMask 方式实现

此种实现方式 调用setMask 让主窗体和异形背景图片形状保持一致。然后再绘制异形背景图片即可。
此种方式需要注意: 需要使得主窗体的大小和 背景图片的大小保持一致,否则会出现错位问题。
关键代码

	_backgroundPixmap = QPixmap("skin/test.png");//异形背景图片
		resize(_backgroundPixmap.size());//让窗体和背景图片大小 否则出现错位问题
	this->setMask(_backgroundPixmap.mask());//让主窗体形状和异形背景图片一致。
	
void FIrregularMaskWidget::paintEvent(QPaintEvent * event)
{
    
    
	QPainter painter(this);
	painter.drawImage(QRect(0, 0, width(), height()), _backgroundPixmap.toImage());
}//绘制异形背景图片

代码

笔者 环境 windwos 7 vs2015 debug 64位
github地址:
https://github.com/1005490940/Qt/tree/master/FIrregularWidget
补充
制作悬浮窗体目前采用move 方式实现。只要计算出绝对位置即可

猜你喜欢

转载自blog.csdn.net/weixin_39308337/article/details/107293017