ValueChanged signal not being emitted in subclass



  • I have created a subclass of QDoubleSpinBox such that it stores a double unadulturated side-by-side a setDecimaled double using the native value for QDoubleSpinBox. The setValue slot is receiving a signal from connected widgets, but the valueChanged signal is not emitting a signal.

    For example, I have a custom double slider (that was working fine with a QDoubleSpinBox) connected to my subclassed QDoubleSpinBox. If I move the slider, the value in the spinbox updates. If I change the spinbox value, the slider does not respond. Same thing if I connect two of my subclassed spinboxes together.

    I know the code is a bit hackish, so I would grateful for any suggestions for improvement.

    Thanks!

    Dave H

    qdpinbox.h:

    @
    #ifndef QDSPINBOX_H
    #define QDSPINBOX_H

    #include <QDoubleSpinBox>

    class QDSpinBox : public QDoubleSpinBox
    {
    Q_OBJECT
    public:
    explicit QDSpinBox();
    explicit QDSpinBox( QWidget * );
    double getRValue() { return rvalue; }
    void setPrecision( int i ) { precision = i; }
    protected:
    double valueFromText( const QString &text ) const;
    QString textFromValue( double value ) const;
    QSize sizeHint() const
    {
    return cachedSizeHint;
    }
    signals:
    void valueChanged( double );
    void valueChanged( QString & );
    public slots:
    void setValue( double );
    void setDecimals( int );
    private:
    int precision;
    double rvalue;
    QSize cachedSizeHint;
    };

    #endif
    @

    qdspinbox.cpp

    @
    #include "qdspinbox.h"

    QDSpinBox::QDSpinBox():QDoubleSpinBox()
    {
    precision = 4;
    cachedSizeHint = QDoubleSpinBox::sizeHint();
    }

    QDSpinBox::QDSpinBox( QWidget *parent = 0 ):QDoubleSpinBox( parent )
    {
    precision = 4;
    cachedSizeHint = QDoubleSpinBox::sizeHint();
    }

    QString QDSpinBox::textFromValue( double value ) const
    {
    return QString::number( value , 'f' , precision );
    }

    double QDSpinBox::valueFromText( const QString &text ) const
    {
    bool ok;
    return text.toDouble( &ok );
    }

    void QDSpinBox::setDecimals( int decimals )
    {
    setValue( rvalue );
    QDoubleSpinBox::setDecimals( decimals );
    emit valueChanged( rvalue );
    }

    void QDSpinBox::setValue( double val )
    {
    if ( rvalue != val )
    {
    rvalue = val;
    QDoubleSpinBox::setValue( val );
    emit valueChanged( value() );
    }
    }
    @


  • Moderators

    My first guess would be that the connect statement does not use your derived class.
    You should post also the connect statement of your signal to whatever slot you are using.



  • When you define own valueChanged( QString & );
    you shadow original QSpinBox::void valueChanged( QString & );
    That's why your slots are not invoked.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.