ismoothpixmaplabel.cpp 1.73 KB
#include "ismoothpixmaplabel.h"
#include <QPainter>
#include <QBitmap>
#include <QSize>
#include <QPixmap>

ISmoothPixmapLabel::ISmoothPixmapLabel(QWidget *parent) :
    QLabel(parent)
{
    isMousein = false;
    setAttribute(Qt::WA_TranslucentBackground);
}
void ISmoothPixmapLabel::resizeEvent(QResizeEvent * event )
{
//  int side = qMin(width(), height());
//  //QRegion maskedRegion(width() / 2 - side / 2, height() / 2 - side / 2, side, side, QRegion::Ellipse);
//  QRegion maskedRegion(this->width()-2, 2, this->width(), this->height(), QRegion::Ellipse);
//  setMask(maskedRegion);
   QPainterPath path;
   QRectF rect = QRectF(0,0,width(),height());
   path.addRoundRect(rect,10,10);
   QPolygon polygon= path.toFillPolygon().toPolygon();//获得这个路径上的所有的点
   QRegion region(polygon);//根据这个点构造这个区域
   setMask(region);
}


void ISmoothPixmapLabel::enterEvent(QEvent *)
{
    if(isMousein)
    {
        return;
    }
    isMousein = true;
    emit mouseIn();

}

void ISmoothPixmapLabel::mousePressEvent(QMouseEvent *ev)
{
    emit mousePress();
}

void ISmoothPixmapLabel::leaveEvent(QEvent *)
{
    isMousein = false;

    emit mouseOut();
}

void ISmoothPixmapLabel::paintEvent(QPaintEvent *event)
{

    if (!event) return;
    QLabel::paintEvent(event);
    QPainter painter(this);

//    painter.setRenderHints(QPainter::SmoothPixmapTransform);//消锯齿

//    QBitmap maskImage("E:\project\LiveChat\LiveChat\bin\debug\img\145766289369756.jpg"); //蒙板图片

//    QPixmap iconImage("E:\project\LiveChat\LiveChat\bin\debug\img\145766289369756.jpg");

//    iconImage.setMask(maskImage);
////    icon = icon.scaled(QSize(54,54));
////    icon.setMask(maskImage);

//    painter.drawPixmap(0, 0, iconImage);
}