国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - PyQt5實現(xiàn)QLineEdit正則表達(dá)式輸入驗證器

PyQt5實現(xiàn)QLineEdit正則表達(dá)式輸入驗證器

2021-10-04 00:27魚子醬126 Python

這篇文章主要介紹了PyQt5實現(xiàn)QLineEdit正則表達(dá)式輸入驗證器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本文主要介紹了QLineEdit正則表達(dá)式輸入驗證器,分享給大家,具體如下:

?
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from PyQt5 import QtWidgets, QtCore, QtGui, Qt
import re
 
############## QLineEdit正則表達(dá)式輸入驗證器
class LineEditRegExpValidator(QtGui.QValidator):
 
  '''
  # 默認(rèn)為科學(xué)計數(shù)法輸入驗證器
 
  用法
  SciNotValidator = LineEditRegExpValidator() # 創(chuàng)建一個QLineEdit正則表達(dá)式輸入驗證器的類,默認(rèn)為科學(xué)計數(shù)法輸入驗證器
 
  self.LineEdit1.setValidator(SciNotValidator) # 設(shè)置驗證器(啟用)
  self.LineEdit1.installEventFilter(SciNotValidator) # QLineEdit清空內(nèi)容且游標(biāo)失焦時,自動填充上一次的字符串內(nèi)容
 
  self.LineEdit2.setValidator(SciNotValidator)
  self.LineEdit2.installEventFilter(SciNotValidator)
 
  self.LineEdit3.setValidator(SciNotValidator)
  self.LineEdit3.installEventFilter(SciNotValidator)
 
  Validator.validate() is abstract and must be overriddenValidator.validate() is abstract and must be overridden
  '''
 
  def __init__(
    self,
 
    # 編輯狀態(tài)框輸入結(jié)束允許的字符串
    fullPatterns=[
      r"[+|-]?[0-9]+\.?[0-9]*(?:[Ee][+|-]?[0-9]+)?",
      r'[+|-]{0,1}nan', r'[+|-]{0,1}inf'
      ],
    
    # 編輯狀態(tài)框輸入尚未結(jié)束允許的字符串
    partialPatterns=[
      r'[+|-]?[0-9]+\.?[0-9]*(?:[Ee][+|-]?)?',
      r'-',
      r'\+',
      r'[+|-]{0,1}nan',
      r'[+|-]{0,1}na',
      r'[+|-]{0,1}n',
      r'[+|-]{0,1}inf',
      r'[+|-]{0,1}in',
      r'[+|-]{0,1}i'
      ],
    
    fixupString='1.0'
    ):
 
    super(LineEditRegExpValidator, self).__init__()
    self.fullPatterns = fullPatterns
    self.partialPatterns = partialPatterns
    self.fixupString = fixupString
  
 
  # 實時監(jiān)聽文本框的改變
  # 可能是鍵盤單個字符'n'輸入, 也有可能是粘貼多個字符'nan'輸入
  def validate(self, string, pos) -> QtGui.QValidator.State: # string為編輯狀態(tài)框中可見的字符串+輸入字符/字符串
 
    # 編輯過程結(jié)束,若返回True,將編輯狀態(tài)框中的字符串填入LineEdit,若返回Flase則自動調(diào)用self.fixup方法,將fixup方法返回的字符串填入LineEdit
    if self.acceptable_check(string):
      #print(f'QtGui.QValidator.Acceptable:{QtGui.QValidator.Acceptable}')
      return QtGui.QValidator.Acceptable, string, pos # QtGui.QValidator.Acceptable = 2;
    
 
    # 編輯過程中允許出現(xiàn)的字符串
    if self.intermediate_check(string):
      #print(f'QtGui.QValidator.Intermediate:{QtGui.QValidator.Intermediate}')
      return QtGui.QValidator.Intermediate, string, pos # QtGui.QValidator.State = 1;
    # 編輯過程中不允許出現(xiàn)的字符串(本次輸入的單個字符或字符串無效)
    else:
      #print(f'QtGui.QValidator.Invalid:{QtGui.QValidator.Invalid}')
      return QtGui.QValidator.Invalid, string, pos
 
 
  # 編輯狀態(tài)框驗證通過, 編輯狀態(tài)框單個字輸入符成功
  def acceptable_check(self, string) -> bool:
    True_ = 0
    for fullPattern in self.fullPatterns:
      if re.fullmatch(fullPattern, string):
        True_ += 1
      else:
        continue
    if True_ != 0:
      return True
    else:
      return False
 
  # 輸入還未結(jié)束允許的字符串
  def intermediate_check(self, string): #-> bool;  string為編輯狀態(tài)框中可見的字符串
    """
    Checks if string makes a valid partial float, keeping in mind locale dependent decimal separators.
    """
    if string == '':
      return True
    for partialPattern in self.partialPatterns:
      if re.fullmatch(partialPattern, string):
        return True
      else:
        pass
 
  #
  def eventFilter(self, lineEdit, event): # -> bool
    # FocusIn event
    # 每當(dāng)fous in時,更新LineEditRegExpValidator的fixupString
    # 輸入驗證器
    '''
    SciNotValidator = LineEditRegExpValidator()
 
    self.LineEdit1.setValidator(SciNotValidator)
    self.LineEdit1.installEventFilter(SciNotValidator)
    '''
 
    if event.type() == QtCore.QEvent.FocusIn:
      # do custom stuff
      # print('focus in')
 
      # self.lineEdit_zhuansu.installEventFilter(SciNotValidator), 在本類中,widget是self.lineEdit,執(zhí)行函數(shù)self.lineEdit.text(), 其它類不一定有text()方法
 
      #lineEdit.selectAll()
      QtCore.QTimer.singleShot(0, lineEdit.selectAll) # 0ms
      self.fixupString = lineEdit.text()
 
      #print(self.fixupString)
      # return False so that the lineEdit will also handle the event
      # otherwise it won't focus out
      return False
    else:
      # we don't care about other events
      return False
 
  # 重寫QValidator的fixup(str)方法。可以在切換焦點后,直接修改不合規(guī)則的字符串。參數(shù)str是經(jīng)過validate()方法驗證后的字符串;
  def fixup(self, string) -> str:
    """
    Fixes up input text to create a valid float. Puts an empty string on failure.
    """
    print(string)
 
    True_ = 0
    for fullPattern in self.fullPatterns:
      if re.fullmatch(fullPattern, string):
        True_ += 1
      else:
        continue
    if True_ != 0:
      return string
    else:
      return self.fixupString
 
 
# listWidget、tableWidget輸入數(shù)據(jù)檢查
class LineEditDelegate_Regx(QtWidgets.QStyledItemDelegate):
 # 科學(xué)計數(shù)法正則表達(dá)式
  regx = r"-?\ *[0-9]+\.?[0-9]*(?:[Ee]\ *-?\ *[0-9]+)?" #
  """
  -?    optionally matches a negative sign (zero or one negative signs)
  \ *    matches any number of spaces (to allow for formatting variations like - 2.3 or -2.3)
  [0-9]+  matches one or more digits
  \.?    optionally matches a period (zero or one periods)
  [0-9]*  matches any number of digits, including zero
  (?: ... ) groups an expression, but without forming a "capturing group" (look it up)
  [Ee]   matches either "e" or "E"
  \ *    matches any number of spaces (to allow for formats like 2.3E5 or 2.3E 5)
  -?    optionally matches a negative sign
  \ *    matches any number of spaces
  [0-9]+  matches one or more digits
  ?     makes the entire non-capturing group optional (to allow for the presence or absence of the exponent - 3000 or 3E3
 
  https://stackoverflow.com/questions/18152597/extract-scientific-number-from-string
  """
 
  """
  用法:
  def __init__(self, parent=None):
    super(NewClassName, self).__init__(parent)
    self.setupUi(self)
 
    delegate = LineEditDelegate_Regx(regx=None)
    self.listWidget_ShuZhiLieBiao.setItemDelegate(delegate)
    self.tableWidget.setItemDelegate(delegate)
  """
  def __init__(self, regx=None, parent=None):
    super(LineEditDelegate_Regx, self).__init__(parent)
    if regx == None:
      pass
    else:
      self.regx = regx
 
  # 方法重寫
  def createEditor(self, parent, option, index): # self, parent, option, index四個參數(shù)均不能少
    editor_qlineedit = QtWidgets.QLineEdit(parent)
    #SciNotValidator = QtGui.QRegExpValidator(QtCore.QRegExp(self.regx))
    SciNotValidator = LineEditRegExpValidator()
    editor_qlineedit.setValidator(SciNotValidator)
    return editor_qlineedit # LineEditDelegate_Regx(regx=None, parent=None), QStyledItemDelegate(parent: QObject = None)
 
"""
# LineEdit輸入數(shù)據(jù)檢查
def LineEditInputChecking(lineEdit, regx=None):
  '''
  用法:
  LineEditInputChecking(lineEdit=self.lineEdit_zhuansu)
  '''
  if regx == None:
    regx = r"-?\ *[0-9]+\.?[0-9]*(?:[Ee]\ *-?\ *[0-9]+)?"
  reg_ex = QtCore.QRegExp(regx)
  input_validator = QtGui.QRegExpValidator(reg_ex, lineEdit)
  lineEdit.setValidator(input_validator)
"""

參考:

https://stackoverflow.com/questions/39202697/qt-qlineedit-input-validation

https://stackoverflow.com/questions/15829782/how-to-restrict-user-input-in-qlineedit-in-pyqt

到此這篇關(guān)于PyQt5實現(xiàn)QLineEdit正則表達(dá)式輸入驗證器的文章就介紹到這了,更多相關(guān)PyQt5 QLineEdit驗證器內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/caviar126/article/details/114959867

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91av视频免费在线观看 | 久久久久久久 | 91久久精品国产91久久性色tv | 欧美激情久久久 | 久久久久久亚洲精品视频 | 欧美日韩一区二区三区免费视频 | 午夜伦理影院 | 久久久五月天 | 亚洲一级在线 | 免费的av网站 | 精久久久 | 免费观看日韩一级片 | 日韩一区二区免费视频 | 日韩精品一区二区在线观看 | 国产日韩精品一区 | 九色国产 | 国产精品影视 | 99热最新 | av在线资源网 | 美女久久久 | 日韩成人在线视频 | 我要看一级黄色 | 国产激情在线看 | 2018天天操| 欧美不卡视频 | 成人免费一区二区三区视频网站 | 久久久成人精品 | a在线看| 久久精品1区 | 亚洲一区二区三区四区五区午夜 | 久久中文字幕一区二区 | 久久精品一级 | av在线一区二区三区 | 91黄色免费视频 | 天天综合7799精品影视 | 欧美三区二区一区 | 国产伦精品一区二区三区精品视频 | 91污在线观看 | 亚洲免费在线观看 | 中文在线播放 | 最近中文字幕mv免费高清在线 |