不规则窗体
- 知识点
- shape.h
- shape.cpp
- main.cpp
- 运行图
知识点
感觉这个就是在图片背景 贴了白色
shape.h
#ifndef SHAPE_H
#define SHAPE_H#include <QWidget>class Shape : public QWidget
{Q_OBJECTpublic:Shape(QWidget *parent = nullptr);~Shape();
protected:void mousePressEvent(QMouseEvent*);void mouseMoveEvent(QMouseEvent *event);void paintEvent(QPaintEvent*);
private:QPoint DragPosition;};
#endif // SHAPE_H
shape.cpp
#include "shape.h"
#include <QMouseEvent>
#include <QPainter>
#include <QPixmap>
#include <QBitmap>Shape::Shape(QWidget *parent): QWidget(parent)
{QPixmap Pix;Pix.load("312.ico",0,Qt::AvoidDither|Qt::ThresholdDither|Qt::ThresholdAlphaDither);resize(Pix.size());setMask(QBitmap(Pix.mask()));}Shape::~Shape() {}void Shape::mousePressEvent(QMouseEvent *Event)
{if(Event->button() == Qt::LeftButton){DragPosition = Event->globalPos() - frameGeometry().topLeft();Event->accept();}if(Event->button() == Qt::RightButton){close();}}void Shape::mouseMoveEvent(QMouseEvent *Event)
{if(Event->buttons()&Qt::LeftButton){move(Event->globalPos()-DragPosition);Event->accept();}
}void Shape::paintEvent(QPaintEvent*)
{QPainter Painter(this);Painter.drawPixmap(0,0,QPixmap("312.ico"));}
main.cpp
#include "shape.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Shape w;w.show();return a.exec();
}