Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] using the same subclass to promote both QSpinBox and QDoubleSpinBox
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 4.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yanbellavance
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yanbellavance
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            yanbellavance
            wrote on last edited by
            #5

            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().

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • Y Offline
                Y Offline
                yanbellavance
                wrote on last edited by
                #7

                Ok. Thanks for your help.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved