[SOLVED] using the same subclass to promote both QSpinBox and QDoubleSpinBox



  • I sub-classed QSpinBox to create a custom widget used to promote QSpinBoxes in Qt Designer. Now, I have to promote a QDoubleSpinbox but this of course does not work with a QSpinBox subclass. I would rather not simply copy and modify the subclass as it is inconveninent to maintain. Is there a way that i could have one set of code for both the QSpinBox subclass and the QDoubleSpinBox subclass. The only difference in the 2 sets is that value is an int in one class and a double ni the other. The conflicting area of code relating to this are the calls to value() which are defined in QDoubleSpinBox and QSpinbox but not QAbstractSpinBox.

    WORKAROUND: Ok I guess I can just use a QDoubleSpinBox with 0 decimals and get rid of QSpinBox:
    @QDoubleSpinBox:setDecimals(0);@

    in the end, this was not possible in my case.



  • Depending on what you are doing exactly, you might be able to use a template class.

    @
    template<class T>
    MySpecialSpinBox: public T
    {
    //can not use Q_OBJECT macro here!
    }

    typedef MySpecialIntSpinBox MySpecialSpinBox<QSpinBox>;
    typedef MySpecialDoubleSpinBox MySpecialSpinBox<QDoubleSpinBox>;

    @

    Or something along those lines. Note that moc does not play nice with templates, so you can do nothing that requires moc in such a template.



  • since I am using:
    @
    value();
    setValue();
    property("newValReady").toBool();
    setProperty("writeEnable",writeEnable);
    QSpinBox::focusInEvent(event);
    QSpinBox::focusOutEvent(event);
    QSpinBox::keyPressEvent(event);
    QAbstractSpinBox::wheelEvent(event);
    etc...
    @
    I don't think it would work



  • Still worth a try, I think. You are not adding new stuff that needs the moc to run: no new signals, no new slots.



  • here is my class header, does it still look feasable? Actually I am going to try it out and see what it gives.

    @class wvSpinBox : public QSpinBox
    {
    Q_OBJECT
    //Q_PROPERTY(bool writeEnable READ writeEnable WRITE setWriteEnable)
    public:
    explicit wvSpinBox(QWidget *parent = 0);
    bool writeEnable() {
    return this->property("writeEnable").toBool();
    }
    void setWriteEnable(const bool & writeEnable){
    this->setProperty("writeEnable",writeEnable);
    }

    bool newValReady() {
        return this->property("newValReady").toBool();
    }
    void setNewValReady(const bool & newValReady){
        this->setProperty("newValReady",newValReady);
    }
    int getNewVal();
    int oldVal;
    

    public slots:
    void when_editingFinished(){

    }
    

    protected:
    void focusInEvent ( QFocusEvent * event );
    void focusOutEvent ( QFocusEvent * event );
    void keyPressEvent ( QKeyEvent * event );
    void wheelEvent ( QWheelEvent * event );
    void mousePressEvent ( QMouseEvent * event );
    void mouseReleaseEvent ( QMouseEvent * event );
    signals:
    void editStart();
    void editStop();
    void updEvent();
    void stepClicked();

    };
    @

    Also, I would be calling value() and setValue() in the template class which are implemented in QDoubleSpinBox and QSpinBox. Would it still compile? I also call property() and setProperty().



  • No, forget it. You have several signals and slots. As I said: the template scenario can only work if you don't need moc on it. For signals and slots, you do.



  • Ok. Thanks for your help.


Log in to reply
 

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