realslider.h
Go to the documentation of this file.
1 #ifndef REALSLIDER_H
2 #define REALSLIDER_H
3 
4 #include <QSlider>
5 
6 
7 class RealSlider: public QSlider
8 {
9  Q_OBJECT
10 public:
11  RealSlider(QWidget* parent = nullptr);
12 
13  void setLimits(double min, double max, int steps);
14  double getValue() const;
15  void setRealValue(double val);
16 
17  double getMaximum() const { return _max_value; }
18 
19  double getMinimum() const { return _min_value; }
20 
21 private slots:
22  void onValueChanged(int value);
23 
24 signals:
25  void realValueChanged(double);
26 
27 private:
28  double _min_value;
29  double _max_value;
30 };
31 //-------------------------------------------------------------
32 
33 inline RealSlider::RealSlider(QWidget *parent):
34  QSlider(parent)
35 {
36  setLimits(0.0, 1.0, 1);
37  connect(this, &QSlider::valueChanged,
39 }
40 
41 inline void RealSlider::setLimits(double min, double max,int steps)
42 {
43  _min_value = min;
44  _max_value = max;
45  QSlider::setRange(0,steps);
46 }
47 
48 inline void RealSlider::setRealValue(double val)
49 {
50  val = std::max( val, _min_value) ;
51  val = std::min( val, _max_value) ;
52  const double ratio = (val -_min_value) / (_max_value - _min_value );
53  long pos = std::round( (double)(maximum() - minimum()) * ratio + minimum()) ;
54  QSlider::setValue(pos);
55 }
56 
57 inline void RealSlider::onValueChanged(int value)
58 {
59  int min = minimum() ;
60  int max = maximum() ;
61  const double ratio = (double)value / (double)(max - min );
62  double posX = (_max_value - _min_value) * ratio + _min_value;
63  emit realValueChanged(posX);
64 }
65 
66 #endif // REALSLIDER_H
double _max_value
Definition: realslider.h:29
double getMinimum() const
Definition: realslider.h:19
double getMaximum() const
Definition: realslider.h:17
void realValueChanged(double)
void setLimits(double min, double max, int steps)
Definition: realslider.h:41
void onValueChanged(int value)
Definition: realslider.h:57
T value
double _min_value
Definition: realslider.h:28
double getValue() const
int min(int a, int b)
void setRealValue(double val)
Definition: realslider.h:48
RealSlider(QWidget *parent=nullptr)
Definition: realslider.h:33


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:18