实现效果
实现过程
不仅仅是圆圈,理论上任何不规则的图形都可以实现出来….
- 新建一个类继承QWidget
- 定义以下方法及成员变量,下述方法均要重写
private:
QPoint dragPosition;
protected:
void mouseMoveEvent(QMouseEvent *);
void mousePressEvent(QMouseEvent *);
void paintEvent(QPaintEvent *);
};
- 对应的cpp代码
#include "splashscreen.h"
#include <QMouseEvent>
#include <QPainter>
#include <QPixmap>
#include <QBitmap>
SplashScreen::SplashScreen(QWidget *parent) : QWidget(parent)
{
QPixmap pix;
pix.load("circle_purple.png",0,Qt::AvoidDither|Qt::ThresholdDither|Qt::ThresholdAlphaDither);//图片路径是build-项目名-Desktop_Qt_5_10_0_MinGW_32bit-Debug
pix = pix.scaled(width(),height(),Qt::KeepAspectRatio);//缩放到与窗口相同
// resize(pix.size());
setMask(QBitmap(pix.mask()));//设置图片遮罩层
}
void SplashScreen::mousePressEvent(QMouseEvent *event){
if(event->button()==Qt::LeftButton){//判断是否是左键点击
dragPosition = event->globalPos()-frameGeometry().topLeft();//计算距离
event->accept();
}else{
//do nothing
close();//关闭窗口
}
}
void SplashScreen::mouseMoveEvent(QMouseEvent *event){
if(event->buttons()&Qt::LeftButton){
move(event->globalPos()-dragPosition);//使窗口移动
event->accept();
}
}
void SplashScreen::paintEvent(QPaintEvent *event){
QPainter painter(this);
QPixmap pix;
pix.load("circle_purple.png",0,Qt::AvoidDither|Qt::ThresholdDither|Qt::ThresholdAlphaDither);
pix = pix.scaled(width(),height(),Qt::KeepAspectRatio);
painter.drawPixmap(0,0,pix);//重写这个函数是为了完成在窗体上绘制图片的工作。用来确定窗体外形的png图片
}
SplashScreen::~SplashScreen()
{
}
OS:那么如何在QSplashScreen中调用这个窗体呢?..