遮罩(Qt)

图片文件:

.h文件

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

protected:
    void paintEvent(QPaintEvent *event)override;//绘图事件
    void mousePressEvent(QMouseEvent* event);//鼠标按下事件

private:
    Ui::Widget *ui;
};

.cpp文件

{
    ui->setupUi(this);

    QPixmap pixmap;
   pixmap.load(":/new/prefix1/flower.png")//图片路径
    
        ui->label->resize(pixmap.size());
       // ui->label->setPixmap(pixmap);
        resize(pixmap.size());
        //设置遮罩
        setMask(pixmap.mask());
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.drawPixmap(0,0,QPixmap(":/new/prefix1/flower.png"));//绘图

}

void Widget::mousePressEvent(QMouseEvent *event)
{
    close();//鼠标按下时关闭
}

 

posted @ 2022-04-20 15:37  乐吴  阅读(243)  评论(0编辑  收藏  举报