I want to connect a QDoubleSpinBox
with a QSlider
like this:
QObject::connect(ui->myDoubleSpinBox, SIGNAL(valueChanged(double)),
ui->mySlider, SLOT(setValue(double)));
QObject::connect(ui->mySlider, SIGNAL(valueChanged(double)),
ui->myDoubleSpinBox, SLOT(setValue(double)));
This won't work, for a QSlider only handles int values. So I think i need to add a custom slot to QSlider.
I thought about creating a new class derived from QSlider and then implementing the slot, like this:
QDoubleSlider.hpp
#ifndef QDOUBLESLIDER_H
#define QDOUBLESLIDER_H
#include <QSlider>
class QDoubleSlider : public QSlider
{
Q_OBJECT
public:
explicit QDoubleSlider(QObject *parent = 0);
signals:
public slots:
void setValue(double givenValue);
};
#endif // QDOUBLESLIDER_H
QDoubleSlider.cpp
#include "qdoubleslider.h"
QDoubleSlider::QDoubleSlider(QObject *parent) :
QSlider(parent)
{
}
void QDoubleSlider::setValue(double givenValue)
{
// code
}
Now I have two problems:
- The compiler complains about an invalid conversion from
QObject*
toQWidget*
in the constructor. - I don't know how setValue works and thus I don't even know how to implement that slot.
Any ideas?
parent
needs to be aQWidget*
, just as the error statesdouble
as a member and set the underlying slider to the appropriate integer equivalent. Remember to only sendvalueChanged
signals if the value really changes.Furthermore you should probably inherit from
QWidget
instead and have aQSlider
as a child, as you do not want users of your class to change the integer range of your internal slider.