-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovableWidget.cpp
60 lines (52 loc) · 1.08 KB
/
MovableWidget.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
#include "MovableWidget.h"
MovableWidget::MovableWidget(QWidget *parent) :
QWidget(parent),
m_bIsMovable(true)
{
this->setWindowFlag(Qt::FramelessWindowHint);
}
void MovableWidget::SetMovable(bool isMovable)
{
m_bIsMovable = isMovable;
}
void MovableWidget::mousePressEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton)
{
m_bLeftPressed = true;
m_pt = event->pos();
}
}
void MovableWidget::mouseMoveEvent(QMouseEvent *event)
{
if (m_bLeftPressed == true)
{
this->moveByDrag(event);
}
}
void MovableWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton)
{
m_bLeftPressed = false;
this->moveByDrag(event);
}
}
void MovableWidget::moveByDrag(QMouseEvent *event)
{
if (m_bIsMovable == false)
{
return;
}
QPoint ptCur;
if (this->parent() == NULL)
{
ptCur = event->screenPos().toPoint() - m_pt;
}
else
{
ptCur = QWidget::mapToParent(event->pos()) - m_pt;
}
this->move(ptCur);
this->repaint();
}