Testing the PySide Validator

vegaseat 1 Tallied Votes 703 Views Share

The PySide/PyQT QValidator restricts the type of input a widget like QLineEdit can accept. Here is a simple test.

''' ps_test_QIntValidator1.py
very simple template to test PySide widgets like
QIntValidator, QLabel, QLineEdit, QVBoxLayout, QGroupBox

PySide is the official LGPL-licensed version of PyQT
from:
http://www.lfd.uci.edu/~gohlke/pythonlibs/
also:
http://qt-project.org/wiki/PySide_Binaries_Windows

for more info see:
http://srinikom.github.com/pyside-docs/PySide/QtGui/QValidator.html

tested with Python27/33 and PySide112  by  vegaseat  07feb2013
'''

from PySide.QtCore import *
from PySide.QtGui import *

# replace [] with sys.argv for commandline
app = QApplication([])

# ----- start your widget test code ----

group_box = QGroupBox("accepts only integers")

label = QLabel()
edit = QLineEdit()

# now edit will only accept integers
edit.setValidator(QIntValidator())

# connect edit to label
# update label each time the text has been edited
edit.textEdited.connect(label.setText)

vbox = QVBoxLayout()
vbox.addWidget(edit)
vbox.addWidget(label)
group_box.setLayout(vbox)

group_box.show()

# ---- end of widget test code -----

app.exec_()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.