-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathCustomSlider.cpp
71 lines (61 loc) · 1.97 KB
/
CustomSlider.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
#include "CustomSlider.h"
#include <QColor>
#include <QSlider>
#include <QStyleOptionComplex>
#include <QStyleOptionSlider>
#include <QStylePainter>
#include "math.h"
CustomSlider::CustomSlider(QWidget* parent) : QSlider(parent) {
this->setStyleSheet(
"\
QSlider {\
min-height: 24px\
}\
QSlider::groove:horizontal {\
border: 1px solid #262626;\
height: 3px;\
background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);\
margin: 0 5px;\
}\
\
QSlider::handle:horizontal {\
background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);\
border: 1px solid #5c5c5c;\
width: 12px;\
margin: -8px -6px;\
border-radius: 3px;\
}\
");
};
void CustomSlider::paintEvent(QPaintEvent* ev) {
QStylePainter p(this);
QStyleOptionSlider opt;
initStyleOption(&opt);
QRect handle = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
// draw tick marks
// do this manually because they are very badly behaved with style sheets
int interval = tickInterval();
if (interval == 0) {
interval = pageStep();
}
if (tickPosition() != NoTicks) {
for (int i = minimum(); i <= maximum(); i += interval) {
int x =
std::round((double)((double)((double)(i - this->minimum()) / (double)(this->maximum() - this->minimum())) *
(double)(this->width() - handle.width()) +
(double)(handle.width() / 2.0))) -
1;
int h = 4;
p.setPen(QColor("#a5a294"));
if (tickPosition() == TicksBothSides || tickPosition() == TicksAbove) {
int y = this->rect().top();
p.drawLine(x, y, x, y + h);
}
if (tickPosition() == TicksBothSides || tickPosition() == TicksBelow) {
int y = this->rect().bottom();
p.drawLine(x, y, x, y - h);
}
}
}
QSlider::paintEvent(ev);
}