#include #include #include template class Slider : public QSlider { public: explicit Slider(QLabel* label, const char *format, T * target, const std::function &convertToT = [](int v){ return T(v); }, const std::function &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 convertToT; std::function convertFromT; };