forked from muziing/PyQt_practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-QAbstractSpinBox-内容验证、信号.py
66 lines (50 loc) · 1.75 KB
/
04-QAbstractSpinBox-内容验证、信号.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sys
from PyQt5.Qt import *
class MyASB(QAbstractSpinBox):
def __init__(self, parent, num: int = 20):
super().__init__(parent)
self.current_num = 20
self.lineEdit().setText(f"{num}")
def stepEnabled(self) -> "QAbstractSpinBox.StepEnabled":
return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
def stepBy(self, steps: int) -> None:
self.current_num = self.current_num + steps
self.lineEdit().setText(str(self.current_num))
def validate(self, input: str, pos: int):
# 18 - 180
try:
num = int(input)
if num < 18:
return QValidator.Intermediate, input, pos
elif num <= 180:
return QValidator.Acceptable, input, pos
else:
return QValidator.Invalid, input, pos
except Exception:
return QValidator.Invalid, input, pos
def fixup(self, input: str) -> str:
self.current_num = 18
return "18"
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QAbstractSpinBox-内容验证")
self.resize(500, 500)
self.move(400, 250)
self.setup_ui()
def setup_ui(self):
asb = MyASB(self)
asb.resize(120, 30)
asb.move(150, 100)
te = QTextEdit(self)
te.resize(200, 80)
te.move(150, 200)
te.setPlaceholderText("将焦点移到这里以实现结束SpinBox的编辑,调用fixup方法")
def te_cao():
te.setText("SpinBox已经结束编辑!")
asb.editingFinished.connect(te_cao)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())