qt_demoe/other/bgdemo/widget.cpp

57 lines
1.6 KiB
C++
Raw Normal View History

2019-10-12 01:00:12 +00:00
#include "widget.h"
#include "ui_widget.h"
#include "qevent.h"
#include "qdebug.h"
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
ui->setupUi(this);
this->setAttribute(Qt::WA_TranslucentBackground);
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);
ui->widget->installEventFilter(this);
2019-11-30 06:27:10 +00:00
ui->widget->setStyleSheet(QString("background-image:url(:/image/%1.png);").arg(1));
2019-10-12 01:00:12 +00:00
}
Widget::~Widget()
{
delete ui;
}
bool Widget::eventFilter(QObject *watched, QEvent *evt)
{
static int index = 1;
static QPoint mousePoint;
static bool mousePressed = false;
QMouseEvent *event = static_cast<QMouseEvent *>(evt);
if (event->type() == QEvent::MouseButtonPress) {
if (event->button() == Qt::LeftButton) {
mousePressed = true;
mousePoint = event->globalPos() - this->pos();
if (index == 5) {
index = 1;
} else {
index++;
}
2019-11-30 06:27:10 +00:00
ui->widget->setStyleSheet(QString("background-image:url(:/image/%1.png);").arg(index));
2019-10-12 01:00:12 +00:00
return true;
} else {
exit(0);
}
} else if (event->type() == QEvent::MouseButtonRelease) {
mousePressed = false;
return true;
} else if (event->type() == QEvent::MouseMove) {
if (mousePressed && (event->buttons() && Qt::LeftButton)) {
this->move(event->globalPos() - mousePoint);
return true;
}
}
return QWidget::eventFilter(watched, event);
}