|
1 | 1 | from PyQt6.QtWidgets import QSpinBox, QApplication |
2 | | -from PyQt6.QtGui import QValidator |
| 2 | +from PyQt6.QtGui import QIntValidator |
3 | 3 | from PyQt6.QtCore import Qt |
4 | 4 |
|
5 | 5 |
|
6 | | -class CustomRangeValidator(QValidator): |
7 | | - def __init__(self, min_val: int, max_val: int, parent=None): |
8 | | - super().__init__(parent) |
9 | | - self.min_val = min_val |
10 | | - self.max_val = max_val |
11 | | - |
12 | | - def validate(self, input_str: str, pos: int): |
13 | | - if input_str == "": |
14 | | - return QValidator.State.Intermediate, input_str, pos |
15 | | - |
16 | | - if input_str.isdigit(): |
17 | | - value = int(input_str) |
18 | | - if self.min_val <= value <= self.max_val: |
19 | | - return QValidator.State.Acceptable, input_str, pos |
20 | | - else: |
21 | | - return QValidator.State.Invalid, input_str, pos |
22 | | - return QValidator.State.Invalid, input_str, pos |
23 | | - |
24 | | - def fixup(self, input_str: str): |
25 | | - return str(self.min_val) |
26 | | - |
27 | | - |
28 | 6 | class SpinBox(QSpinBox): |
29 | 7 | def __init__(self, parent=None): |
30 | 8 | super().__init__(parent) |
31 | | - self.validator = CustomRangeValidator(self.minimum(), self.maximum(), self) |
32 | | - self.lineEdit().setValidator(self.validator) |
33 | 9 | self.lineEdit().inputRejected.connect(QApplication.beep) |
34 | 10 |
|
35 | | - def update_validator(self): |
36 | | - self.validator.min_val = self.minimum() |
37 | | - self.validator.max_val = self.maximum() |
38 | | - |
39 | 11 | def focusInEvent(self, event): |
40 | 12 | super().focusInEvent(event) |
41 | 13 | self.lineEdit().selectAll() |
0 commit comments