-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
90 lines (77 loc) · 2.36 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QMovie>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>
#include <QVBoxLayout>
#include <QCursor>
#include <QMouseEvent>
#include <QPoint>
#include <QIcon>
#include <QPixmap>
class GifWidget : public QWidget {
public:
GifWidget(const QString& gifPath) {
initUI(gifPath);
}
private:
void quit() {
close();
qApp->quit();
}
void initUI(const QString& gifPath) {
setWindowFlags(Qt::FramelessWindowHint | Qt::SubWindow);
setAutoFillBackground(false);
setAttribute(Qt::WA_TranslucentBackground, true);
QVBoxLayout* layout = new QVBoxLayout(this);
QLabel* label = new QLabel(this);
layout->addWidget(label);
setLayout(layout);
QMovie* movie = new QMovie(gifPath);
movie->setSpeed(100);
label->setMovie(movie);
movie->start();
QPixmap pixmap(":/Skd/data/icon.jpg");
QAction* quitAction = new QAction("quit", this);
quitAction->setIcon(QIcon(pixmap));
connect(quitAction, &QAction::triggered, this, &GifWidget::quit);
QMenu* trayIconMenu = new QMenu(this);
trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(QIcon(pixmap), this);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->show();
}
void mousePressEvent(QMouseEvent* event) override {
if (event->button() == Qt::LeftButton) {
isFollowMouse = true;
mouseDragPos = event->globalPos() - pos();
event->accept();
setCursor(QCursor(Qt::OpenHandCursor));
}
}
void mouseMoveEvent(QMouseEvent* event) override {
if (isFollowMouse && (event->buttons() & Qt::LeftButton)) {
move(event->globalPos() - mouseDragPos);
event->accept();
}
}
void mouseReleaseEvent(QMouseEvent* event) override {
if (event->button() == Qt::LeftButton) {
isFollowMouse = false;
setCursor(QCursor(Qt::ArrowCursor));
}
}
private:
bool isFollowMouse = false;
QPoint mouseDragPos;
QSystemTrayIcon* trayIcon;
};
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QString gifPath = ":/Skd/data/skd.gif";
GifWidget widget(gifPath);
widget.show();
return app.exec();
}