You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
919 B
37 lines
919 B
#include <QSlider>
|
|
#include <functional>
|
|
#include <QLabel>
|
|
|
|
template<typename T = int>
|
|
class Slider : public QSlider {
|
|
public:
|
|
explicit Slider(QLabel* label, const char *format, T * target,
|
|
const std::function<T(int)> &convertToT = [](int v){ return T(v); },
|
|
const std::function<int(T)> &convertFromT = [](T v){ return int(v); }) : QSlider(Qt::Horizontal) {
|
|
this->convertToT = convertToT;
|
|
this->convertFromT = convertFromT;
|
|
this->target = target;
|
|
|
|
connect(this, &QSlider::valueChanged, this, [=, this](int newValue){
|
|
T convertedValue = convertToT(newValue);
|
|
|
|
char buff[100];
|
|
snprintf(buff, sizeof(buff), format, convertedValue);
|
|
label->setText(buff);
|
|
|
|
targetOld = *target;
|
|
*target = convertedValue;
|
|
});
|
|
}
|
|
|
|
void setFromTarget(){
|
|
setValue(convertFromT(*target));
|
|
}
|
|
|
|
T targetOld;
|
|
|
|
private:
|
|
T * target;
|
|
std::function<T(int)> convertToT;
|
|
std::function<int(T)> convertFromT;
|
|
}; |