Custom slider initialization and interval problem
-
I finally have my subclass of QSlider working and have it linked to a QDSpinBox. My problem is now that despite having the two linked using connect, the initial values given to the QDSpinBox are not passed to the QDSlider (my subclass of QSlider).
Also, the step interval for my QDSlider is supposed to be set at 5 from what I entered in using QtDesigner, but it appears to move at intervals of 1.
Here's the relevant code:
qdslider.h:
@
#include <QSlider>class QDSlider : public QSlider
{
Q_OBJECT
public:
explicit QDSlider();
explicit QDSlider( QWidget * );
explicit QDSlider( Qt::Orientation , QWidget * );
signals:
void valueChanged( double );
void sliderMoved( double );
private slots:
void setValue( double );
void valueDChanged( int );
};
@qdslider.cpp:
@
#include "qdslider.h"QDSlider::QDSlider():QSlider() {}
QDSlider::QDSlider( QWidget *parent = 0 ):QSlider( parent ) {}
QDSlider::QDSlider( Qt::Orientation orientation , QWidget *parent = 0 ):QSlider( orientation , parent ) {}void QDSlider::setValue( double value )
{
int ivalue = value * 10;
emit QSlider::setValue( ivalue );
}void QDSlider::valueDChanged( int value )
{
double dvalue = (double)value / 10.0;
emit valueChanged( dvalue );
}
@connect code in main:
@
// Connect the two slider slots
QObject::connect( ui->eptErrorHSlider , SIGNAL( valueChanged( int ) ) ,
ui->eptErrorHSlider , SLOT( valueDChanged( int ) ) );
QObject::connect( ui->eptErrorDSpin , SIGNAL( valueChanged( double ) ) ,
ui->eptErrorHSlider , SLOT( setValue( double ) ) );
QObject::connect( ui->eptErrorHSlider , SIGNAL( sliderMoved( double ) ) ,
ui->eptErrorDSpin , SLOT( setValue( double ) ) );
QObject::connect( ui->eptErrorHSlider , SIGNAL( valueChanged( double ) ) ,
ui->eptErrorDSpin , SLOT( setValue( double ) ) );
QObject::connect( ui->eptPowerHSlider , SIGNAL( valueChanged( int ) ) ,
ui->eptPowerHSlider , SLOT( valueDChanged( int ) ) );
QObject::connect( ui->eptPowerDSpin , SIGNAL( valueChanged( double ) ) ,
ui->eptPowerHSlider , SLOT( setValue( double ) ) );
QObject::connect( ui->eptPowerHSlider , SIGNAL( sliderMoved( double ) ) ,
ui->eptPowerDSpin , SLOT( setValue( double ) ) );
QObject::connect( ui->eptPowerHSlider , SIGNAL( valueChanged( double ) ) ,
ui->eptPowerDSpin , SLOT( setValue( double ) ) );
@Any ideas on what I'm doing wrong?
Dave H
-
Hi,
I have found some bugs, but I am not sure, whether they help:
@
void QDSlider::setValue( double value )
{
int ivalue = value * 10;
emit QSlider::setValue( ivalue );
}
@here emit is wrong, setValue is not a signal.
Why do you connect the signals of your base class to your slot from outside and not within your constructor? That makes much more sense
-
I personally would create a new custom widget, containing the slider and the spin box, with it's own set of signals and slots and "hide" the internal connections between the both in the custom widget. You can even make a plugin to use the new custom widget in Qt Designer.