使用setmask实现不规则窗体

来源:互联网 发布:gta5美女捏脸数据最新 编辑:程序博客网 时间:2024/05/16 04:02

setmask为调用它的空间添加一个遮罩,在这个遮罩范围之外的都没有显示,但是窗体大小不会变化。

#ifndef SHAPEWIDGET_H#define SHAPEWIDGET_H#include <QWidget>#include <QBitmap>#include <QApplication>#include <QMouseEvent>#include <Qpainter>class ShapeWidget : public QWidget{    Q_OBJECTpublic:    explicit ShapeWidget(QWidget *parent = 0);    signals:    public slots:protected:    void mousePressEvent(QMouseEvent *e);    void mouseMoveEvent(QMouseEvent *e);    void paintEvent(QPaintEvent *);private:    QPixmap pix;    QPoint point;    };#endif // SHAPEWIDGET_H

#include "shapewidget.h"#include <QDebug>ShapeWidget::ShapeWidget(QWidget *parent) :    QWidget(parent,Qt::FramelessWindowHint){    qDebug() << size();    pix.load(":/temp.png",0,Qt::AvoidDither             | Qt::ThresholdAlphaDither             | Qt::ThresholdDither);    setMask(pix.mask());    qDebug() << size();}void ShapeWidget::mousePressEvent(QMouseEvent *e){    if (e->button() == Qt::LeftButton) {        point = e->globalPos() - frameGeometry().topLeft();        e->accept();    }    if (e->button() == Qt::RightButton) {        close();    }}void ShapeWidget::mouseMoveEvent(QMouseEvent *e){    //Note that the returned value is always Qt::NoButton    //for mouse move events.    //    if (e->buttons() & Qt::LeftButton) {        move(e->globalPos() - point);        e->accept();    }}void ShapeWidget::paintEvent(QPaintEvent *){    QPainter p(this);    p.drawPixmap(0,0,pix);}

#include <QApplication>#include "shapewidget.h"int main(int argc, char* argv[]){    QApplication app(argc,argv);    ShapeWidget sw;    sw.show();    return app.exec();}

移动的时候计算使用的是相对于左上角的点。添加了右键功能。

使用的是从图片获得的一个遮掩,对于图片就是它的透明的部分



原创粉丝点击