-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gavin Yang
committed
Jun 7, 2020
1 parent
5dde6f4
commit 3f5c58e
Showing
20 changed files
with
1,233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include "DropShadowWidget.h" | ||
|
||
#include <QPainter> | ||
#include <QtMath> | ||
#include <QMouseEvent> | ||
#include <QDebug> | ||
|
||
#define SHADOW_WIDTH 10 | ||
|
||
struct FramelessWindowPrivate { | ||
FramelessWindowPrivate(QWidget *contentWidget) : contentWidget(contentWidget) {} | ||
QWidget *contentWidget; | ||
QPoint mousePressedPosition; // 鼠标按下时的坐标 | ||
QPoint windowPositionAsDrag; // 鼠标按小时窗口左上角的坐标 | ||
}; | ||
|
||
DropShadowWidget::DropShadowWidget(QWidget *parent) | ||
: QWidget(parent) | ||
, mIsValidArea(false) | ||
{ | ||
setWindowOpacity(0.7); | ||
setWindowFlags(Qt::FramelessWindowHint); // 去掉边框 | ||
setAttribute(Qt::WA_TranslucentBackground); // 背景透明 | ||
setAttribute(Qt::WA_TransparentForMouseEvents); | ||
setMouseTracking(true); | ||
|
||
d = new FramelessWindowPrivate(this); | ||
|
||
mBorderPath.addRoundRect(SHADOW_WIDTH, SHADOW_WIDTH, width() - SHADOW_WIDTH*2, height() - SHADOW_WIDTH*2, 10); | ||
} | ||
|
||
DropShadowWidget::~DropShadowWidget() | ||
{ | ||
|
||
} | ||
|
||
void DropShadowWidget::paintEvent(QPaintEvent *event) | ||
{ | ||
Q_UNUSED(event); | ||
QPainterPath path; | ||
path.setFillRule(Qt::WindingFill); | ||
path.addRoundRect(SHADOW_WIDTH, SHADOW_WIDTH, width() - SHADOW_WIDTH*2, height() - SHADOW_WIDTH*2, 10); | ||
// path.addRect(SHADOW_WIDTH, SHADOW_WIDTH, width() - SHADOW_WIDTH*2, height() - SHADOW_WIDTH*2); | ||
QPainter painter(this); | ||
painter.setRenderHint(QPainter::Antialiasing, true); | ||
painter.fillPath(path, QBrush(Qt::white)); | ||
|
||
QColor color(255, 0, 0, 50); | ||
for(int i = 0; i < SHADOW_WIDTH; i++) | ||
{ | ||
QPainterPath path; | ||
path.setFillRule(Qt::WindingFill); | ||
// path.addRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, width()-(SHADOW_WIDTH-i)*2, height()-(SHADOW_WIDTH-i)*2); | ||
path.addRoundRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, width()-(SHADOW_WIDTH-i)*2, height()-(SHADOW_WIDTH-i)*2, 10); | ||
color.setAlpha(150 - qSqrt(i)*50); | ||
painter.setPen(color); | ||
painter.drawPath(path); | ||
} | ||
} | ||
|
||
void DropShadowWidget::mousePressEvent(QMouseEvent *e) | ||
{ | ||
if(mBorderPath.contains(e->localPos())){ | ||
qDebug() << "contains" << e->localPos(); | ||
mIsValidArea = true; | ||
d->mousePressedPosition = e->globalPos(); | ||
d->windowPositionAsDrag = pos(); | ||
setCursor(Qt::ClosedHandCursor); | ||
}else{ | ||
mIsValidArea = false; | ||
setCursor(Qt::ArrowCursor); | ||
} | ||
} | ||
|
||
void DropShadowWidget::mouseReleaseEvent(QMouseEvent *e) | ||
{ | ||
Q_UNUSED(e) | ||
// 鼠标放开始设置鼠标按下的位置为 null,表示鼠标没有被按下 | ||
d->mousePressedPosition = QPoint(); | ||
} | ||
|
||
void DropShadowWidget::mouseMoveEvent(QMouseEvent *e) | ||
{ | ||
qDebug() << "mouseMoveEvent, " << e; | ||
if (mIsValidArea && !d->mousePressedPosition.isNull()) { | ||
// 鼠标按下并且移动时,移动窗口, 相对于鼠标按下时的位置计算,是为了防止误差累积 | ||
QPoint delta = e->globalPos() - d->mousePressedPosition; | ||
move(d->windowPositionAsDrag + delta); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef DROPSHADOWWIDGET_H | ||
#define DROPSHADOWWIDGET_H | ||
|
||
#include <QWidget> | ||
|
||
struct FramelessWindowPrivate; | ||
class DropShadowWidget : public QWidget | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
DropShadowWidget(QWidget *parent = 0); | ||
~DropShadowWidget(); | ||
protected: | ||
void paintEvent(QPaintEvent *event) override; | ||
void mousePressEvent(QMouseEvent *e) override; | ||
void mouseReleaseEvent(QMouseEvent *e) override; | ||
void mouseMoveEvent(QMouseEvent *e) override; | ||
private: | ||
FramelessWindowPrivate *d; | ||
QPainterPath mBorderPath; | ||
bool mIsValidArea; | ||
}; | ||
|
||
#endif // DROPSHADOWWIDGET_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#------------------------------------------------- | ||
# | ||
# Project created by QtCreator 2017-04-16T15:11:34 | ||
# | ||
#------------------------------------------------- | ||
|
||
QT += core gui | ||
CONFIG += console | ||
|
||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | ||
|
||
TARGET = DropShadowWidget | ||
TEMPLATE = app | ||
|
||
|
||
SOURCES += main.cpp\ | ||
DropShadowWidget.cpp | ||
|
||
HEADERS += DropShadowWidget.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# DropShadowWidget | ||
this is a frameless transparent window with drop shadow. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "DropShadowWidget.h" | ||
#include <QApplication> | ||
#include <QDebug> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
QApplication a(argc, argv); | ||
|
||
// qDebug() << QString::fromLocal8Bit("一去丶二三里"); | ||
// qDebug() << QString::fromLocal8Bit("青春不老,奋斗不止!"); | ||
// qDebug() << QString::fromLocal8Bit("纯正开源之美,有趣、好玩、靠谱。。。"); | ||
|
||
DropShadowWidget w; | ||
w.show(); | ||
|
||
return a.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "MainWindow.h" | ||
#include "ui_MainWindow.h" | ||
|
||
MainWindow::MainWindow(QWidget *parent) : | ||
QMainWindow(parent), | ||
ui(new Ui::MainWindow) | ||
{ | ||
ui->setupUi(this); | ||
} | ||
|
||
MainWindow::~MainWindow() | ||
{ | ||
delete ui; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef MAINWINDOW_H | ||
#define MAINWINDOW_H | ||
|
||
#include <QMainWindow> | ||
|
||
namespace Ui { | ||
class MainWindow; | ||
} | ||
|
||
class MainWindow : public QMainWindow | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit MainWindow(QWidget *parent = 0); | ||
~MainWindow(); | ||
|
||
private: | ||
Ui::MainWindow *ui; | ||
}; | ||
|
||
#endif // MAINWINDOW_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>MainWindow</class> | ||
<widget class="QMainWindow" name="MainWindow"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>400</width> | ||
<height>300</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>MainWindow</string> | ||
</property> | ||
<widget class="QWidget" name="centralWidget"> | ||
<widget class="QRoundProgressBar" name="roundprogressbar" native="true"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>50</x> | ||
<y>30</y> | ||
<width>151</width> | ||
<height>101</height> | ||
</rect> | ||
</property> | ||
</widget> | ||
<widget class="QProgressBar" name="redProgressBar"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>70</x> | ||
<y>200</y> | ||
<width>251</width> | ||
<height>23</height> | ||
</rect> | ||
</property> | ||
<property name="styleSheet"> | ||
<string notr="true">QProgressBar[redProgressBar = "true"]{ | ||
color : solid gray; | ||
border: 2px solid gray; | ||
border-radius: 5px; | ||
background: transparent; | ||
padding: 0px; | ||
text-align : center ; | ||
} | ||
QProgressBar[redProgressBar = "true"]::chunk{ | ||
background: #B22222; | ||
}</string> | ||
</property> | ||
<property name="value"> | ||
<number>24</number> | ||
</property> | ||
</widget> | ||
</widget> | ||
<widget class="QMenuBar" name="menuBar"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>400</width> | ||
<height>17</height> | ||
</rect> | ||
</property> | ||
</widget> | ||
<widget class="QToolBar" name="mainToolBar"> | ||
<attribute name="toolBarArea"> | ||
<enum>TopToolBarArea</enum> | ||
</attribute> | ||
<attribute name="toolBarBreak"> | ||
<bool>false</bool> | ||
</attribute> | ||
</widget> | ||
<widget class="QStatusBar" name="statusBar"/> | ||
</widget> | ||
<layoutdefault spacing="6" margin="11"/> | ||
<customwidgets> | ||
<customwidget> | ||
<class>QRoundProgressBar</class> | ||
<extends>QWidget</extends> | ||
<header>QRoundProgressBar.h</header> | ||
<container>1</container> | ||
</customwidget> | ||
</customwidgets> | ||
<resources/> | ||
<connections/> | ||
</ui> |
Oops, something went wrong.