-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtitlebar.cc
More file actions
53 lines (46 loc) · 1.46 KB
/
Copy pathtitlebar.cc
File metadata and controls
53 lines (46 loc) · 1.46 KB
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
#include "titlebar.h"
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QPushButton>
TitleBar::TitleBar(const QString &title, QWidget *parent)
: QWidget(parent)
{
setObjectName("titleBar");
setFixedHeight(38);
auto *layout = new QHBoxLayout(this);
layout->setContentsMargins(16, 0, 8, 0);
layout->setSpacing(0);
titleLabel = new QLabel(title, this);
titleLabel->setObjectName("titleBarText");
layout->addWidget(titleLabel);
layout->addStretch();
auto *closeButton = new QPushButton(QString(QChar(0x00D7)), this);
closeButton->setObjectName("titleBarCloseButton");
closeButton->setFixedSize(44, 28);
closeButton->setCursor(Qt::PointingHandCursor);
closeButton->setFocusPolicy(Qt::NoFocus);
connect(closeButton, &QPushButton::clicked, this, [this] {
if (auto *w = window()) {
w->close();
}
});
layout->addWidget(closeButton);
}
void TitleBar::setTitle(const QString &title)
{
titleLabel->setText(title);
}
void TitleBar::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && window() != nullptr) {
dragPosition = event->globalPosition().toPoint() - window()->frameGeometry().topLeft();
event->accept();
}
}
void TitleBar::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) && window() != nullptr) {
window()->move(event->globalPosition().toPoint() - dragPosition);
event->accept();
}
}