6 #include "percentslider.h"
9 static QString
format(int value
)
11 return QLocale::system().toString(value
) + UNIT_SPACE
+ QString("%");
14 PercentSlider::PercentSlider(QWidget
*parent
) : QWidget(parent
)
16 _slider
= new QSlider(Qt::Horizontal
);
17 _label
= new QLabel();
19 _slider
->setMinimum(0);
20 _slider
->setMaximum(100);
22 QFontMetrics
fm(_label
->font());
23 _label
->setFixedWidth(fm
.boundingRect(format(_slider
->maximum())).width());
24 _label
->setAlignment(Qt::AlignRight
);
25 _label
->setText(format(_slider
->value()));
27 connect(_slider
, &QSlider::valueChanged
, this, &PercentSlider::updateLabel
);
29 QHBoxLayout
*layout
= new QHBoxLayout();
30 layout
->addWidget(_slider
);
31 layout
->addWidget(_label
);
36 void PercentSlider::updateLabel(int value
)
38 _label
->setText(format(value
));
41 int PercentSlider::value() const
43 return _slider
->value();
46 void PercentSlider::setValue(int value
)
48 _slider
->setValue(value
);
49 _label
->setText(format(value
));