問(wèn)題描述
這是一個(gè)關(guān)于輸入驗(yàn)證的兩部分問(wèn)題,其中包含一個(gè)特定組件和另一個(gè)更通用的組件.
This is a two part question about input validation with one specific and another more general component.
具體:
在研究該主題時(shí),我發(fā)現(xiàn) THIS on Regular表達(dá)式.我意識(shí)到這篇文章中的代碼使用的是 PyQt4.但是我想讓它與 PyQt5 一起工作,因?yàn)槲乙呀?jīng)用它開始了我的項(xiàng)目.(顯然是盲目的——我只能找到它的 C++ 文檔)
While researching the topic, I found THIS on Regular Expressions. I realize that the code in this post is using PyQt4. However I wanted to get this working with PyQt5, since I had already started my project with it. (Obviously blindly - I can only find C++ documentation for it)
這是我嘗試過(guò)的:
# somewhere above:
self.le_input = QtWidgets.QLineEdit()
# at some point validate_input gets called:
# more on that in the second part of this question
def validate_input(self):
reg_ex = QtCore.QRegExp(""[0-9]+.?[0-9]{,2}"")
input_validator = QtGui.QRegExpValidator(reg_ex, self.le_input.text())
self.le_input.setValidator(input_validator)
當(dāng)我運(yùn)行代碼時(shí),出現(xiàn)以下錯(cuò)誤:
When I run the code I get the following Error:
QRegExpValidator(parent: QObject = None): 參數(shù) 1 具有意外類型 'QRegExp'QRegExpValidator(QRegExp, parent: QObject = None): 參數(shù) 2 具有意外類型 'str'
QRegExpValidator(parent: QObject = None): argument 1 has unexpected type 'QRegExp' QRegExpValidator(QRegExp, parent: QObject = None): argument 2 has unexpected type 'str'
這些不正是需要的參數(shù)嗎?有誰(shuí)知道如何讓這個(gè)工作?
Aren't these exactly the required arguments? Does anyone know how to get this working?
將軍:
在 Python 中使用 PyQt 實(shí)現(xiàn)實(shí)時(shí)驗(yàn)證的有效方法是什么?
What is an effective way to implement live validation with PyQt in Python?
目前我會(huì)使用:
self.le_input.textChanged.connect(self.validate_input)
這確實(shí)有效,但是一旦我嘗試將兩個(gè)相互影響的 QtLineEdits 連接到同一個(gè)插槽,事情就會(huì)停止工作,因?yàn)樗鼈兺瑫r(shí)調(diào)用了textChanged".
This does work, but as soon as I try to connect two QtLineEdits, that affect each other, to the same slot, things stop working because "textChanged" gets called by both of them at the same time.
用例:兩個(gè)輸入字段:Amount before TAX
和 Amount after TAX
- 輸入時(shí)自動(dòng)填充另一個(gè)字段.
Use case: Two input fields: Amount before TAX
and Amount after TAX
- and whichever you enter automatically fills the other one while typing.
先驗(yàn)證,再計(jì)算,再輸出到其他字段.
First validation, then calculation, then output to the other field.
提前非常感謝!非常感謝任何幫助!
推薦答案
你可以為不同的QLineEdit
對(duì)象設(shè)置不同的驗(yàn)證器.
You can set different validators for different QLineEdit
objects.
QRegExpValidator
有兩個(gè)構(gòu)造函數(shù):
QRegExpValidator(parent: QObject = None)
QRegExpValidator(QRegExp, parent: QObject = None)
所以,您應(yīng)該將代碼更改為:
so, you should change your code to:
reg_ex = QRegExp("[0-9]+.?[0-9]{,2}")
input_validator = QRegExpValidator(reg_ex, self.le_input)
self.le_input.setValidator(input_validator)
一旦為 QLineEdit 設(shè)置了驗(yàn)證器,就不需要使用了
Once you set validator for QLineEdit, there is no need to use
self.le_input.textChanged.connect(self.validate_input)
刪除即可.這應(yīng)該可以正常工作.
Just delete it. And that should work fine.
如果您想查找有關(guān) PyQt5 的文檔,您可以在控制臺(tái)中簡(jiǎn)單地使用它:
If you want to find documentation about PyQt5, you can simple use this in your console:
pydoc3 PyQt5.QtGui.QRegExpValidator
但是 pydoc 只能顯示很少的信息,所以你也應(yīng)該使用 C++ 版本的文檔.
But pydoc can only show you little information, so you should use C++ version documation as well.
最后,舉個(gè)例子:
from PyQt5.Qt import QApplication
from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QRegExpValidator
from PyQt5.QtWidgets import QWidget, QLineEdit
import sys
class MyWidget(QWidget):
def __init__(self, parent=None):
super(QWidget, self).__init__(parent)
self.le_input = QLineEdit(self)
reg_ex = QRegExp("[0-9]+.?[0-9]{,2}")
input_validator = QRegExpValidator(reg_ex, self.le_input)
self.le_input.setValidator(input_validator)
if __name__ == '__main__':
a = QApplication(sys.argv)
w = MyWidget()
w.show()
a.exec()
這篇關(guān)于PyQt5 和 Python 中的用戶輸入驗(yàn)證的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!