-
Notifications
You must be signed in to change notification settings - Fork 0
/
custombox.cpp
47 lines (40 loc) · 1.18 KB
/
custombox.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
// custombox.cpp
#include "custombox.h"
#include <QMouseEvent>
CustomBox::CustomBox(QWidget *parent)
: QWidget(parent),
isDragging(false)
{
setAutoFillBackground(true);
setPalette(QPalette(QColor(0, 0, 255))); // RGB values for blue
setCursor(Qt::PointingHandCursor);
}
void CustomBox::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
isDragging = true;
lastMousePos = event->globalPos();
}
}
void CustomBox::mouseMoveEvent(QMouseEvent *event)
{
if (isDragging) {
int newX = x() + event->globalX() - lastMousePos.x();
int newY = y() + event->globalY() - lastMousePos.y();
// Limit the movement within the window boundaries
newX = qBound(0, newX, parentWidget()->width() - width());
newY = qBound(0, newY, parentWidget()->height() - height());
move(newX, newY);
lastMousePos = event->globalPos();
}
}
void CustomBox::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
isDragging = false;
}
}
void CustomBox::adjustSize()
{
// This function is empty as we don't want to change the size when the window is resized
}