diff --git a/README.md b/README.md index 103df38..5c14b7f 100644 --- a/README.md +++ b/README.md @@ -16,4 +16,5 @@ Qt编写的一些开源的demo,包括串口调试助手、网络调试助手 | 10 | navbutton | 导航按钮控件 | | 11 | video_splite | 视频监控画面分割demo | | 12 | framelesswidget | 通用无边框拖动拉伸类 | -| 13 | ipaddress | IP地址输入控件 | \ No newline at end of file +| 13 | ipaddress | IP地址输入控件 | +| 14 | bgdemo | 无边框背景透明窗体 | \ No newline at end of file diff --git a/bgdemo/1.png b/bgdemo/1.png new file mode 100644 index 0000000..8c7ff76 Binary files /dev/null and b/bgdemo/1.png differ diff --git a/bgdemo/2.png b/bgdemo/2.png new file mode 100644 index 0000000..eecf851 Binary files /dev/null and b/bgdemo/2.png differ diff --git a/bgdemo/3.png b/bgdemo/3.png new file mode 100644 index 0000000..ec1f3c6 Binary files /dev/null and b/bgdemo/3.png differ diff --git a/bgdemo/4.png b/bgdemo/4.png new file mode 100644 index 0000000..47c5514 Binary files /dev/null and b/bgdemo/4.png differ diff --git a/bgdemo/5.png b/bgdemo/5.png new file mode 100644 index 0000000..daba510 Binary files /dev/null and b/bgdemo/5.png differ diff --git a/bgdemo/bgdemo.pro b/bgdemo/bgdemo.pro new file mode 100644 index 0000000..5396028 --- /dev/null +++ b/bgdemo/bgdemo.pro @@ -0,0 +1,23 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2017-08-19T20:24:03 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = bgdemo +TEMPLATE = app + + +SOURCES += main.cpp\ + widget.cpp + +HEADERS += widget.h + +FORMS += widget.ui + +RESOURCES += \ + rc.qrc diff --git a/bgdemo/main.cpp b/bgdemo/main.cpp new file mode 100644 index 0000000..90b6d53 --- /dev/null +++ b/bgdemo/main.cpp @@ -0,0 +1,11 @@ +#include "widget.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + + return a.exec(); +} diff --git a/bgdemo/rc.qrc b/bgdemo/rc.qrc new file mode 100644 index 0000000..5adf8d7 --- /dev/null +++ b/bgdemo/rc.qrc @@ -0,0 +1,9 @@ + + + 1.png + 2.png + 3.png + 4.png + 5.png + + diff --git a/bgdemo/widget.cpp b/bgdemo/widget.cpp new file mode 100644 index 0000000..5125c7a --- /dev/null +++ b/bgdemo/widget.cpp @@ -0,0 +1,56 @@ +#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); + ui->widget->setStyleSheet(QString("background-image: url(:/%1.png);").arg(1)); +} + +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(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++; + } + + ui->widget->setStyleSheet(QString("background-image: url(:/%1.png);").arg(index)); + + 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); +} diff --git a/bgdemo/widget.h b/bgdemo/widget.h new file mode 100644 index 0000000..e9f1b4b --- /dev/null +++ b/bgdemo/widget.h @@ -0,0 +1,25 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include + +namespace Ui { +class Widget; +} + +class Widget : public QWidget +{ + Q_OBJECT + +public: + explicit Widget(QWidget *parent = 0); + ~Widget(); + +protected: + bool eventFilter(QObject *watched, QEvent *evt); + +private: + Ui::Widget *ui; +}; + +#endif // WIDGET_H diff --git a/bgdemo/widget.ui b/bgdemo/widget.ui new file mode 100644 index 0000000..014bf85 --- /dev/null +++ b/bgdemo/widget.ui @@ -0,0 +1,44 @@ + + + Widget + + + + 0 + 0 + 712 + 353 + + + + Widget + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + + + + + + + +