tools
読み取り中…
検索中…
一致する文字列を見つけられません
doubleSlider.cpp
[詳解]
1#include "doubleSlider.h"
2#include <QtWidgets/QStyleOptionSlider>
3
4DoubleSlider::DoubleSlider(Qt::Orientation orientation, QWidget *parent)
5 : QSlider(orientation, parent), m_lowerValue(0), m_upperValue(maximum()) {}
6
7int DoubleSlider::lowerValue() const {
8 return m_lowerValue;
9}
10
11int DoubleSlider::upperValue() const {
12 return m_upperValue;
13}
14
15void DoubleSlider::setLowerValue(int value) {
16 if (value < minimum()) value = minimum();
17 if (value > m_upperValue) value = m_upperValue;
18 m_lowerValue = value;
19 update();
20}
21
22void DoubleSlider::setUpperValue(int value) {
23 if (value > maximum()) value = maximum();
24 if (value < m_lowerValue) value = m_lowerValue;
25 m_upperValue = value;
26 update();
27}
28
29void DoubleSlider::paintEvent(QPaintEvent *event) {
30 QSlider::paintEvent(event);
31 QPainter painter(this);
32
33 QStyleOptionSlider opt;
34 initStyleOption(&opt);
35
36 QRect groove = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
37 QRect handleLower = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
38 QRect handleUpper = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
39
40 handleLower.moveLeft(lowerValueToPos());
41 handleUpper.moveLeft(upperValueToPos());
42
43 painter.fillRect(groove, Qt::gray);
44 painter.fillRect(handleLower, Qt::blue);
45 painter.fillRect(handleUpper, Qt::blue);
46}
47
48int DoubleSlider::lowerValueToPos() const {
49 return static_cast<int>((m_lowerValue - minimum()) / static_cast<double>(maximum() - minimum()) * width());
50}
51
52int DoubleSlider::upperValueToPos() const {
53 return static_cast<int>((m_upperValue - minimum()) / static_cast<double>(maximum() - minimum()) * width());
54}
int lowerValue
下限ハンドルの値
int upperValue
上限ハンドルの値
DoubleSlider(Qt::Orientation orientation, QWidget *parent=nullptr)
コンストラクタ
void paintEvent(QPaintEvent *event) override
ペイントイベント。2つのハンドルをスロット上に描画する
上下限の2点を持つ Qt スライダーウィジェット