Vincent Kane
Vincent Kane

Reputation: 43

How to change QDoubleSpinBox to Custome Spin Box, while keeping the QT Designer Formatting?

Using QT Designer, I built a user interface with a few QDoubleSpinBoxes. I want to change the spin boxes so that only the left most digit is incremented. For example, if the user presses the up button, 1000 goes to 2000. But if the down button is pressed, 1000 should go to 900. I created a custom Spin Box Class that overwrote the stepBy function.

I'm not sure how to replace the QDoubleSpinBoxes with the Custom Spin Box while keeping all the formatting I already placed on the QDoubleSpinBoxes in QT Designer. I know that you can promote the QDoubleSpinBoxes but I'm not sure how to connect that with my python code.

I tried changing the singleStepSize whenever the value was changed but that lead to 1000 going to 0. But I want 1000 to 900.

Upvotes: 1

Views: 305

Answers (2)

Vincent Kane
Vincent Kane

Reputation: 43

  1. In Qt designer, open the UI file and promote the widget.
  2. Create New Promoted Class
  3. I set the Name to: JaceSpinBox
  4. I set the header file without the .h extention: JaceSpinClass
  5. In python make the custome QWidget Class. Use the promoted class name. (JaceSpinBox)
  6. Imbed Custom widget inside of an object class. Use the header file name. (JaceSpinClass)
  7. In the python script load the custom widget before the ui file.

'''

class JaceSpinClass(object):
    """This is stored in sys modules to be found when the UI is loaded"""
    class JaceSpinBox(QDoubleSpinBox):
        def __init__(self, *args):
            QDoubleSpinBox.__init__(self, *args)

        def stepBy(self, steps):
            # I put my custome function here
            super().stepBy(steps)

class MyWindow(QWidget):
    def __init__(self):
        # Loads UI here
        ui_suruga_control, _ = loadUiType(r"path_to_ui\my.ui")
        super(MyWindow, self).__init__()
        self.ui = ui_suruga_control()
        self.ui.setupUi(self)


if __name__ == "__main__":

    # Load object into system modules before loading the UI 
    sys.modules['JaceSpinClass'] = JaceSpinClass 
    

    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

'''

Upvotes: 0

Marco F.
Marco F.

Reputation: 705

Option 1: Promote the QDoubleSpinBox in the designer to your custom class. Set the QDoubleSpinBox as base class. Check the box for "Global include" and empty the "Header file" entry. Make sure you include your custom spinbox before the ui. Keep in mind, this way you can not use your custom functionality in the preview from the designer.

Option 2: Write a plugin https://doc.qt.io/qt-6/designer-creating-custom-widgets.html

Upvotes: 0

Related Questions