新增加无边框背景透明窗体

master
feiyangqingyun 2019-10-12 09:00:12 +08:00
parent 2a5256d1c6
commit bb1695c544
11 changed files with 168 additions and 0 deletions

BIN
bgdemo/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 KiB

BIN
bgdemo/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

BIN
bgdemo/3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 KiB

BIN
bgdemo/4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

BIN
bgdemo/5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

23
bgdemo/bgdemo.pro Normal file
View File

@ -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

11
bgdemo/main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}

9
bgdemo/rc.qrc Normal file
View File

@ -0,0 +1,9 @@
<RCC>
<qresource prefix="/">
<file>1.png</file>
<file>2.png</file>
<file>3.png</file>
<file>4.png</file>
<file>5.png</file>
</qresource>
</RCC>

56
bgdemo/widget.cpp Normal file
View File

@ -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<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++;
}
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);
}

25
bgdemo/widget.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
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

44
bgdemo/widget.ui Normal file
View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>712</width>
<height>353</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QWidget" name="widget" native="true">
<property name="styleSheet">
<string notr="true"/>
</property>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>