Reputation: 43
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
Reputation: 43
'''
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
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