Qt Irregular Shaped Form Implementation

Adopt the main window transparent way to realize

Specific implementation ideas:
first remove the title header of the form, set the main form to the transparent property, and then set a form to display the special-shaped picture.
Key code

	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 来显示异形背景图片

Implemented using setMask

In this way, setMask is called to keep the shape of the main window consistent with that of the special-shaped background image. Then draw a special-shaped background picture.
Pay attention to this method: The size of the main window and the size of the background image need to be kept the same, otherwise, misalignment will occur.
Key code

	_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());
}//绘制异形背景图片

Code

The author's environment windwos 7 vs2015 debug 64-bit
github address:
https://github.com/1005490940/Qt/tree/master/FIrregularWidget
Supplement The
production of floating forms is currently implemented by move. Just calculate the absolute position

Guess you like

Origin blog.csdn.net/weixin_39308337/article/details/107293017