【问题标题】:I use QDoubleValidator in my pyqt5 program but it doesn't seem to work我在我的 pyqt5 程序中使用 QDoubleValidator 但它似乎不起作用
【发布时间】:2019-07-11 11:24:07
【问题描述】:

我创建了一个 QWidget 对象,其中有一些 lineEdits,我打算给它们添加一些约束,所以我实现了 QDoubleValidator 对象。 以下是我的代码中的相关部分。

self.lineEdit_taxRate= QLineEdit()
self.lineEdit_taxRate.setValidator(QDoubleValidator(0.0, 100.0, 6))

但是当我运行程序时,我发现我仍然可以输入像 123165.15641 这样的数字。看来验证器没什么区别。

我想知道我错过了哪一步或者验证器会触发一些信号。

线条编辑

【问题讨论】:

    标签: python python-3.x pyqt pyqt5 qlineedit


    【解决方案1】:

    默认情况下,QDoubleValidator 使用 ScientificNotation notation,并且在该表示法中 123165.15641 是一个可能的有效值,因为它可以转换为 123165.15641E-100,这是一个介于 0 和 100 之间的数字。在这种情况下,解决方案是确定它使用的是标准符号:

    from PyQt5 import QtGui, QtWidgets
    
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.lineEdit_taxRate= QtWidgets.QLineEdit()
            self.lineEdit_taxRate.setValidator(
                QtGui.QDoubleValidator(
                    0.0, # bottom
                    100.0, # top
                    6, # decimals 
                    notation=QtGui.QDoubleValidator.StandardNotation
                )
            )
            self.setCentralWidget(self.lineEdit_taxRate)
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        w = MainWindow()
        w.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 2020-06-28
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多