My problem is that I just can't seem to make my QSlider work with double values instead of integer, because I need to make it return double values to a QLineEdit and also set it's own value when I put some value in the edit.
Asked
Active
Viewed 2.3k times
2 Answers
10
When I was a Qt beginner I started with this tutorial. It is a little bit old (it refers to Qt4.1), but it was good enough to get me started!
I have put together a simple example application that can show you where to start... Maybe you can find it helpful!
#include <QApplication>
#include <QtGui>
#include <QVBoxLayout>
#include <QSlider>
#include <QLabel>
class DoubleSlider : public QSlider {
Q_OBJECT
public:
DoubleSlider(QWidget *parent = 0) : QSlider(parent) {
connect(this, SIGNAL(valueChanged(int)),
this, SLOT(notifyValueChanged(int)));
}
signals:
void doubleValueChanged(double value);
public slots:
void notifyValueChanged(int value) {
double doubleValue = value / 10.0;
emit doubleValueChanged(doubleValue);
}
};
class Test : public QWidget {
Q_OBJECT
public:
Test(QWidget *parent = 0) : QWidget(parent),
m_slider(new DoubleSlider()),
m_label(new QLabel())
{
m_slider->setOrientation(Qt::Horizontal);
m_slider->setRange(0, 100);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(m_slider);
layout->addWidget(m_label);
connect(m_slider, SIGNAL(doubleValueChanged(double)),
this, SLOT(updateLabelValue(double)));
updateLabelValue(m_slider->value());
}
public slots:
void updateLabelValue(double value) {
m_label->setText(QString::number(value, 'f', 2));
}
private:
QSlider *m_slider;
QLabel *m_label;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test *wid = new Test();
wid->show();
return a.exec();
}
#include "main.moc"

William Spinelli
- 429
- 3
- 7
7
You can simply divide slider value on some constant. For example:
const int dpi = 10; // Any constant 10^n
int slideVal = 57; // Integer value from slider
double realVal = double( slideVal / dpi ); // float value

Dmitry Sazonov
- 8,801
- 1
- 35
- 61
-
Well, I've seen this solution before, and I found it's the easiest, but my teacher wanted me to practice signals and slots, so I'd prefer to adding custom signals and slots. But thank you anyway! – Isaac Kennedy Sep 25 '13 at 11:42
-
@user2814864 your teacher's idea is good as well. You could create your own `QObject` class that will re-translate `QSlider`'s signals, but with `double` arguments. – vahancho Sep 25 '13 at 11:45
-
@vahancho exactly, but the problem is, I'm still a beginner in both programming and Qt, and I don't have real ideas on how to do that. – Isaac Kennedy Sep 25 '13 at 11:48
-
@vahancho Ok! I'll give it some shots, but do you know if the documentation could help me with it? – Isaac Kennedy Sep 25 '13 at 11:53
-
Start with official documentation. Did you read any? - http://qt-project.org/doc/qt-5.0/qtcore/signalsandslots.html – Dmitry Sazonov Sep 25 '13 at 12:12
-
@DmitrySazonov no, I didn't. Thank you man! I think that this documentation will be very usefull. – Isaac Kennedy Sep 25 '13 at 12:22