设置窗口无边框
anomaly::anomaly(QWidget *parent) : QWidget(parent)
{
//去边框
this->setWindowFlags(Qt::FramelessWindowHint | windowFlags());
resize(300,300);
//背景透明
setAttribute(Qt::WA_TranslucentBackground);
}
重写事件函数
因为无边框,为了达到窗口可以拖动的目的。重新实现鼠标按下,鼠标移动事件。
void anomaly::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.drawPixmap(0,0,QPixmap("../../images/bg.jpg"));
}
void anomaly::mousePressEvent(QMouseEvent *e)
{
if(e->button()== Qt::LeftButton)
{
point=e->globalPos()-this->frameGeometry().topLeft();
}
else if (e->button()== Qt::RightButton)
{
close();
}
}
void anomaly::mouseMoveEvent(QMouseEvent *e)
{
if(e->buttons() & Qt::LeftButton)
{
move(e->globalPos()-point) ;
}
}