Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to construct a doubleSpinBox with default minimum and maximum value



  • This is the class header file for a doubleSpin box which i am inheriting from QDoubleSpinBox.

    currently after creating the box i have to set minimum to 0.0 and maximum value to 9999.0 explicitly
    When ever i create DoubleSpinBox thee values should be set automatically.

    class SumDoubleBox : public QDoubleSpinBox
    {
    	Q_OBJECT
    public:
    	using QDoubleSpinBox::QDoubleSpinBox;  // inherit c'tors
    	// re-implement to keep track of default step (optional, could hard-code step in stepBy())
    	void setSingleStep(double val);
    	// override to adjust step size
    	double singleStep() const;
    
    	void stepBy(int steps) override;		
    
    private:
    	double m_defaultStep = 1.0;
    	double m_CurrentStep;
    	bool m_stepUp;
    };
    

  • Lifetime Qt Champion

    Hi
    Cant you just set the wanted value in the constructor ?



  • @mrjj can you please show the code line which will set the minimum value for the SpinBox


  • Lifetime Qt Champion

    @summit
    It would just be in ctor.
    setMinimum( 0.0 );
    setMaximum(9999.0);

    like

     explicit SumDoubleBox(QWidget *parent = nullptr) : QDoubleSpinBox(parent)
        {
            setMinimum(0.0);
            setMaximum(9999.0);
        }
    
    


  • @mrjj Just one more query currently i am inheriting constructors using using QDoubleSpinBox::QDoubleSpinBox; // inherit c'tors

    so in this case i would not need this line of code ?


  • Lifetime Qt Champion

    @summit
    well you dont need that anymore since you provide your own and call the one you normally inherited.

    explicit SumDoubleBox(QWidget *parent = nullptr) : QDoubleSpinBox(parent) << call base !
    {
    setMinimum(0.0);
    setMaximum(9999.0);
    }

    so

    class SumDoubleBox : public QDoubleSpinBox
    {
        Q_OBJECT
    public:
        //using QDoubleSpinBox::QDoubleSpinBox;  // inherit c'tors
    
        explicit SumDoubleBox(QWidget *parent = nullptr) : QDoubleSpinBox(parent)
        {
            setMinimum(0.0);
            setMaximum(9999.0);
        }
    ....
    
    


  • @mrjj Thank you very much for your kind help as usual .


Log in to reply