fix input validation to work with custom widgets

This commit is contained in:
Roland W-H 2025-02-17 10:05:38 +00:00
parent 68ac992483
commit 679a509101
2 changed files with 22 additions and 15 deletions

View File

@ -84,6 +84,9 @@
<property name="prefix"> <property name="prefix">
<string>£</string> <string>£</string>
</property> </property>
<property name="maximum">
<double>999.000000000000000</double>
</property>
</widget> </widget>
</item> </item>
<item row="2" column="3"> <item row="2" column="3">
@ -107,6 +110,9 @@
<property name="prefix"> <property name="prefix">
<string>£</string> <string>£</string>
</property> </property>
<property name="maximum">
<double>999.000000000000000</double>
</property>
</widget> </widget>
</item> </item>
<item row="8" column="0" colspan="2"> <item row="8" column="0" colspan="2">
@ -137,6 +143,9 @@
<property name="prefix"> <property name="prefix">
<string>£</string> <string>£</string>
</property> </property>
<property name="maximum">
<double>999.000000000000000</double>
</property>
</widget> </widget>
</item> </item>
<item row="6" column="0" colspan="2"> <item row="6" column="0" colspan="2">
@ -157,6 +166,9 @@
<property name="correctionMode"> <property name="correctionMode">
<enum>QAbstractSpinBox::CorrectionMode::CorrectToNearestValue</enum> <enum>QAbstractSpinBox::CorrectionMode::CorrectToNearestValue</enum>
</property> </property>
<property name="maximum">
<number>999</number>
</property>
</widget> </widget>
</item> </item>
<item row="8" column="3"> <item row="8" column="3">
@ -210,6 +222,9 @@
<property name="suffix"> <property name="suffix">
<string>%</string> <string>%</string>
</property> </property>
<property name="maximum">
<double>99.000000000000000</double>
</property>
</widget> </widget>
</item> </item>
<item row="0" column="0" colspan="2"> <item row="0" column="0" colspan="2">
@ -233,6 +248,9 @@
<property name="prefix"> <property name="prefix">
<string>£</string> <string>£</string>
</property> </property>
<property name="maximum">
<double>999.000000000000000</double>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>

View File

@ -1,6 +1,6 @@
from PyQt6.QtCore import QRegularExpression, QEvent, QObject, QTimer from PyQt6.QtCore import QRegularExpression
from PyQt6.QtGui import QRegularExpressionValidator from PyQt6.QtGui import QRegularExpressionValidator
from PyQt6.QtWidgets import QWidget, QDoubleSpinBox from PyQt6.QtWidgets import QWidget
from PyQt6 import uic from PyQt6 import uic
import main_window import main_window
@ -63,15 +63,13 @@ class PlatformEdit(QWidget):
# Handle events # Handle events
for field in self.required_fields: for field in self.required_fields:
field.valueChanged.connect(self.check_valid) field.valueChanged.connect(self.check_valid)
field.installEventFilter(self)
for field in self.optional_fields: for field in self.optional_fields:
field_type = field.staticMetaObject.className() field_type = field.staticMetaObject.className()
if field_type == "QLineEdit": if field_type == "QLineEdit":
field.textChanged.connect(self.check_valid) field.textChanged.connect(self.check_valid)
elif field_type == "QDoubleSpinBox" or field_type == "QSpinBox": elif field_type == "FastEditQDoubleSpinBox" or field_type == "FastEditQSpinBox":
field.valueChanged.connect(self.check_valid) field.valueChanged.connect(self.check_valid)
field.installEventFilter(self)
for check_box in self.optional_check_boxes: for check_box in self.optional_check_boxes:
check_box.checkStateChanged.connect(self.check_valid) check_box.checkStateChanged.connect(self.check_valid)
@ -108,15 +106,6 @@ class PlatformEdit(QWidget):
# Once user input is received show main window # Once user input is received show main window
self.main_win.show() self.main_win.show()
# When focus is given to an input box, select all text in it (easier to edit)
def focusInEvent(self, event):
print("yay")
if event.gotFocus():
# Alternative condition for % suffix - currently unused
#if obj.value() == 0 or obj == self.share_plat_fee_box:
QTimer.singleShot(0, sender.selectAll)
return False
# This method does multiple things in order to validate the user's inputs: # This method does multiple things in order to validate the user's inputs:
# 1) Check all required fields have a non-zero value # 1) Check all required fields have a non-zero value
# 2) If an optional checkbox is toggled: toggle editing of the corresponding field # 2) If an optional checkbox is toggled: toggle editing of the corresponding field
@ -149,7 +138,7 @@ class PlatformEdit(QWidget):
if input_box_type == "QLineEdit": if input_box_type == "QLineEdit":
if input_box_item.text() == "": if input_box_item.text() == "":
valid = False valid = False
elif input_box_type == "QDoubleSpinBox" or input_box_type == "QSpinBox": elif input_box_type == "FastEditQDoubleSpinBox" or input_box_type == "FastEditQSpinBox":
if input_box_item.value() == 0: if input_box_item.value() == 0:
valid = False valid = False
else: else: