tools
読み取り中…
検索中…
一致する文字列を見つけられません
doubleSlider.h
[詳解]
1#pragma once
2#include <QtWidgets/QSlider>
3#include <QtGui/QPainter>
4#include <QtGui/QMouseEvent>
5#include <QtWidgets/QStyle>
6
7/**
8 * @file qt/doubleSlider.h
9 * @brief 上下限の2点を持つ Qt スライダーウィジェット
10 *
11 * QSlider を継承し、選択範囲の下限値と上限値の2つのハンドルを
12 * 描画するダブルレンジスライダーを提供します。
13 */
14
15/**
16 * @class DoubleSlider
17 * @brief 下限・上限の2値を同時に表示・操作できるスライダーウィジェット
18 *
19 * lowerValue と upperValue の2つのパブリックメンバを持ち、
20 * paintEvent でそれぞれの位置に青いハンドルを描画します。
21 *
22 * @note 現バージョンではマウス操作によるハンドルの移動は未実装です
23 *
24 * @code
25 * auto* slider = new DoubleSlider(Qt::Horizontal, this);
26 * slider->lowerValue = 20;
27 * slider->upperValue = 80;
28 * @endcode
29 */
30class DoubleSlider : public QSlider {
31 Q_OBJECT
32
33public:
34 /**
35 * @brief コンストラクタ
36 * @param orientation スライダーの向き(Qt::Horizontal / Qt::Vertical)
37 * @param parent 親ウィジェット(デフォルト: nullptr)
38 */
39 DoubleSlider(Qt::Orientation orientation, QWidget *parent = nullptr)
40 : QSlider(orientation, parent), lowerValue(0), upperValue(maximum()) {}
41
42 int lowerValue; ///< 下限ハンドルの値
43 int upperValue; ///< 上限ハンドルの値
44
45protected:
46 /**
47 * @brief ペイントイベント。2つのハンドルをスロット上に描画する
48 * @param event ペイントイベント(基底クラスに転送される)
49 */
50 void paintEvent(QPaintEvent *event) override {
51 QSlider::paintEvent(event);
52 QPainter painter(this);
53 QRect groove = style()->subControlRect(QStyle::CC_Slider, &option(), QStyle::SC_SliderGroove, this);
54 QRect handleLower = style()->subControlRect(QStyle::CC_Slider, &option(), QStyle::SC_SliderHandle, this);
55 QRect handleUpper = style()->subControlRect(QStyle::CC_Slider, &option(), QStyle::SC_SliderHandle, this);
56
57 handleLower.moveLeft(lowerValueToPos());
58 handleUpper.moveLeft(upperValueToPos());
59
60 painter.fillRect(groove, Qt::gray);
61 painter.fillRect(handleLower, Qt::blue);
62 painter.fillRect(handleUpper, Qt::blue);
63 }
64
65private:
66 /**
67 * @brief lowerValue をスライダー上のピクセル位置に変換する
68 * @return lowerValue に対応する X 座標(ピクセル)
69 */
70 int lowerValueToPos() const {
71 return static_cast<int>((lowerValue - minimum()) / static_cast<double>(maximum() - minimum()) * width());
72 }
73
74 /**
75 * @brief upperValue をスライダー上のピクセル位置に変換する
76 * @return upperValue に対応する X 座標(ピクセル)
77 */
78 int upperValueToPos() const {
79 return static_cast<int>((upperValue - minimum()) / static_cast<double>(maximum() - minimum()) * width());
80 }
81};
int lowerValue
下限ハンドルの値
int upperValue
上限ハンドルの値
DoubleSlider(Qt::Orientation orientation, QWidget *parent=nullptr)
コンストラクタ
void paintEvent(QPaintEvent *event) override
ペイントイベント。2つのハンドルをスロット上に描画する