| 1 |
#ifndef MYSLIDER_H |
| 2 |
#define MYSLIDER_H |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#pragma once |
| 8 |
|
| 9 |
#include <QStylePainter> |
| 10 |
#include <QStyleOptionSlider> |
| 11 |
#include <QStyleOptionComplex> |
| 12 |
#include <QSlider> |
| 13 |
#include <QColor> |
| 14 |
#include "math.h" |
| 15 |
|
| 16 |
class MySlider:public QSlider |
| 17 |
{ |
| 18 |
public: |
| 19 |
explicit MySlider(Qt::Orientation orientation, QWidget *parent = nullptr):QSlider(orientation, parent){}; |
| 20 |
explicit MySlider(QWidget *parent = nullptr):QSlider(parent){ |
| 21 |
this->setStyleSheet(R"( |
| 22 |
|
| 23 |
QSlider::groove:horizontal { |
| 24 |
height: 2px; |
| 25 |
background: #dcdcdc; |
| 26 |
border: 1px solid #a8a8a8; |
| 27 |
border-radius: 1px; |
| 28 |
} |
| 29 |
|
| 30 |
QSlider::sub-page:horizontal { |
| 31 |
background: #aedaf5; |
| 32 |
border: 1px solid #a8a8a8; |
| 33 |
height: 4px; |
| 34 |
border-radius: 2px; |
| 35 |
} |
| 36 |
|
| 37 |
QSlider::handle:horizontal { |
| 38 |
background: #e9eaeb; |
| 39 |
border: 1px solid #a8a8a8; |
| 40 |
width: 18px; |
| 41 |
height: 18px; |
| 42 |
margin: -9px -1px; |
| 43 |
border-radius: 9px; |
| 44 |
padding: -9px 0px; |
| 45 |
} |
| 46 |
|
| 47 |
QSlider::groove:vertical { |
| 48 |
width: 2px; |
| 49 |
background: #dcdcdc; |
| 50 |
border: 1px solid #a8a8a8; |
| 51 |
border-radius: 1px; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
QSlider::add-page:vertical { |
| 56 |
background: #aedaf5; |
| 57 |
border: 1px solid #a8a8a8; |
| 58 |
width: 4px; |
| 59 |
border-radius: 2px; |
| 60 |
} |
| 61 |
|
| 62 |
QSlider::handle:vertical { |
| 63 |
background: #e9eaeb; |
| 64 |
border: 1px solid #a8a8a8; |
| 65 |
width: 18px; |
| 66 |
height: 18px; |
| 67 |
margin: -1 -9px; |
| 68 |
border-radius: 9px; |
| 69 |
} |
| 70 |
)"); |
| 71 |
}; |
| 72 |
protected: |
| 73 |
virtual void paintEvent(QPaintEvent *ev) |
| 74 |
{ |
| 75 |
QStylePainter p(this); |
| 76 |
QStyleOptionSlider opt; |
| 77 |
initStyleOption(&opt); |
| 78 |
|
| 79 |
QRect handle = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
int interval = tickInterval(); |
| 84 |
if (interval == 0) |
| 85 |
{ |
| 86 |
interval = pageStep(); |
| 87 |
} |
| 88 |
|
| 89 |
if (tickPosition() != NoTicks) |
| 90 |
{ |
| 91 |
for (int i = minimum(); i <= maximum(); i += interval) |
| 92 |
{ |
| 93 |
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; |
| 94 |
int h = 4; |
| 95 |
p.setPen(QColor(0xa5, 0xa2, 0x94)); |
| 96 |
if (tickPosition() == TicksBothSides || tickPosition() == TicksAbove) |
| 97 |
{ |
| 98 |
int y = this->rect().top(); |
| 99 |
p.drawLine(x, y, x, y + h); |
| 100 |
} |
| 101 |
if (tickPosition() == TicksBothSides || tickPosition() == TicksBelow) |
| 102 |
{ |
| 103 |
int y = this->rect().bottom(); |
| 104 |
p.drawLine(x, y, x, y - h); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
QSlider::paintEvent(ev); |
| 110 |
} |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
#endif |