-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cpp
75 lines (57 loc) · 1.97 KB
/
MainWindow.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
#include "MainWindow.h"
#include <QPushButton>
#include "Ini.h"
#define wWidth 150
#define wHeight 150
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), dragging(false) {
connect(this, &MainWindow::updateLabel, this, &MainWindow::handleUpdateLabel);
setWindowFlags(Qt::ToolTip);
// setAttribute(Qt::WA_TranslucentBackground);
label = new QLabel("", this);
label->resize(wWidth, wHeight);
// this->setStyleSheet("QMainWindow {background-image:url(:/image/resources/bg.png)}");
QPalette qp = this->palette();
QImage ai(":/image/resources/bg.png");
ai = ai.scaled(wWidth, wHeight, Qt::IgnoreAspectRatio);
qp.setBrush(QPalette::Window, QBrush(ai));
this->setPalette(qp);
Ini i("conf.ini");
int x = i.readInt("pos", "x", 0);
int y = i.readInt("pos", "y", 0);
resize(wWidth, wHeight);
move(x, y);
label->show();
}
MainWindow::~MainWindow() {
}
void MainWindow::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
dragging = true;
dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
event->accept();
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *event) {
if (event->buttons() & Qt::LeftButton && dragging) {
move(event->globalPosition().toPoint() - dragPosition);
event->accept();
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event) {
dragging = false;
std::cout << event->pos().x() << "," << event->pos().y() << std::endl;
Ini i("conf.ini");
i.writeInt("pos", "x", static_cast<int>(event->globalPosition().x()) - this->width());
i.writeInt("pos", "y", static_cast<int>(event->globalPosition().y()) - this->height());
event->accept();
}
void MainWindow::handleUpdateLabel(const std::string &id, const QString &text) {
map[id] = text;
QString buf;
for (const auto &item: map) {
buf.append(item.second);
}
label->setText(buf);
label->update();
}