1#!/usr/bin/env python 2 3 4############################################################################# 5## 6## Copyright (C) 2013 Riverbank Computing Limited. 7## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 8## All rights reserved. 9## 10## This file is part of the examples of PyQt. 11## 12## $QT_BEGIN_LICENSE:BSD$ 13## You may use this file under the terms of the BSD license as follows: 14## 15## "Redistribution and use in source and binary forms, with or without 16## modification, are permitted provided that the following conditions are 17## met: 18## * Redistributions of source code must retain the above copyright 19## notice, this list of conditions and the following disclaimer. 20## * Redistributions in binary form must reproduce the above copyright 21## notice, this list of conditions and the following disclaimer in 22## the documentation and/or other materials provided with the 23## distribution. 24## * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor 25## the names of its contributors may be used to endorse or promote 26## products derived from this software without specific prior written 27## permission. 28## 29## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 40## $QT_END_LICENSE$ 41## 42############################################################################# 43 44 45from PyQt5.QtCore import Qt 46from PyQt5.QtGui import QDoubleValidator, QIntValidator 47from PyQt5.QtWidgets import (QApplication, QComboBox, QGridLayout, QGroupBox, 48 QLabel, QLineEdit, QWidget) 49 50 51class Window(QWidget): 52 def __init__(self): 53 super(Window, self).__init__() 54 55 echoGroup = QGroupBox("Echo") 56 57 echoLabel = QLabel("Mode:") 58 echoComboBox = QComboBox() 59 echoComboBox.addItem("Normal") 60 echoComboBox.addItem("Password") 61 echoComboBox.addItem("PasswordEchoOnEdit") 62 echoComboBox.addItem("No Echo") 63 64 self.echoLineEdit = QLineEdit() 65 self.echoLineEdit.setFocus() 66 67 validatorGroup = QGroupBox("Validator") 68 69 validatorLabel = QLabel("Type:") 70 validatorComboBox = QComboBox() 71 validatorComboBox.addItem("No validator") 72 validatorComboBox.addItem("Integer validator") 73 validatorComboBox.addItem("Double validator") 74 75 self.validatorLineEdit = QLineEdit() 76 77 alignmentGroup = QGroupBox("Alignment") 78 79 alignmentLabel = QLabel("Type:") 80 alignmentComboBox = QComboBox() 81 alignmentComboBox.addItem("Left") 82 alignmentComboBox.addItem("Centered") 83 alignmentComboBox.addItem("Right") 84 85 self.alignmentLineEdit = QLineEdit() 86 87 inputMaskGroup = QGroupBox("Input mask") 88 89 inputMaskLabel = QLabel("Type:") 90 inputMaskComboBox = QComboBox() 91 inputMaskComboBox.addItem("No mask") 92 inputMaskComboBox.addItem("Phone number") 93 inputMaskComboBox.addItem("ISO date") 94 inputMaskComboBox.addItem("License key") 95 96 self.inputMaskLineEdit = QLineEdit() 97 98 accessGroup = QGroupBox("Access") 99 100 accessLabel = QLabel("Read-only:") 101 accessComboBox = QComboBox() 102 accessComboBox.addItem("False") 103 accessComboBox.addItem("True") 104 105 self.accessLineEdit = QLineEdit() 106 107 echoComboBox.activated.connect(self.echoChanged) 108 validatorComboBox.activated.connect(self.validatorChanged) 109 alignmentComboBox.activated.connect(self.alignmentChanged) 110 inputMaskComboBox.activated.connect(self.inputMaskChanged) 111 accessComboBox.activated.connect(self.accessChanged) 112 113 echoLayout = QGridLayout() 114 echoLayout.addWidget(echoLabel, 0, 0) 115 echoLayout.addWidget(echoComboBox, 0, 1) 116 echoLayout.addWidget(self.echoLineEdit, 1, 0, 1, 2) 117 echoGroup.setLayout(echoLayout) 118 119 validatorLayout = QGridLayout() 120 validatorLayout.addWidget(validatorLabel, 0, 0) 121 validatorLayout.addWidget(validatorComboBox, 0, 1) 122 validatorLayout.addWidget(self.validatorLineEdit, 1, 0, 1, 2) 123 validatorGroup.setLayout(validatorLayout) 124 125 alignmentLayout = QGridLayout() 126 alignmentLayout.addWidget(alignmentLabel, 0, 0) 127 alignmentLayout.addWidget(alignmentComboBox, 0, 1) 128 alignmentLayout.addWidget(self.alignmentLineEdit, 1, 0, 1, 2) 129 alignmentGroup. setLayout(alignmentLayout) 130 131 inputMaskLayout = QGridLayout() 132 inputMaskLayout.addWidget(inputMaskLabel, 0, 0) 133 inputMaskLayout.addWidget(inputMaskComboBox, 0, 1) 134 inputMaskLayout.addWidget(self.inputMaskLineEdit, 1, 0, 1, 2) 135 inputMaskGroup.setLayout(inputMaskLayout) 136 137 accessLayout = QGridLayout() 138 accessLayout.addWidget(accessLabel, 0, 0) 139 accessLayout.addWidget(accessComboBox, 0, 1) 140 accessLayout.addWidget(self.accessLineEdit, 1, 0, 1, 2) 141 accessGroup.setLayout(accessLayout) 142 143 layout = QGridLayout() 144 layout.addWidget(echoGroup, 0, 0) 145 layout.addWidget(validatorGroup, 1, 0) 146 layout.addWidget(alignmentGroup, 2, 0) 147 layout.addWidget(inputMaskGroup, 0, 1) 148 layout.addWidget(accessGroup, 1, 1) 149 self.setLayout(layout) 150 151 self.setWindowTitle("Line Edits") 152 153 def echoChanged(self, index): 154 if index == 0: 155 self.echoLineEdit.setEchoMode(QLineEdit.Normal) 156 elif index == 1: 157 self.echoLineEdit.setEchoMode(QLineEdit.Password) 158 elif index == 2: 159 self.echoLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit) 160 elif index == 3: 161 self.echoLineEdit.setEchoMode(QLineEdit.NoEcho) 162 163 def validatorChanged(self, index): 164 if index == 0: 165 self.validatorLineEdit.setValidator(0) 166 elif index == 1: 167 self.validatorLineEdit.setValidator(QIntValidator(self.validatorLineEdit)) 168 elif index == 2: 169 self.validatorLineEdit.setValidator(QDoubleValidator(-999.0, 999.0, 2, self.validatorLineEdit)) 170 171 self.validatorLineEdit.clear() 172 173 def alignmentChanged(self, index): 174 if index == 0: 175 self.alignmentLineEdit.setAlignment(Qt.AlignLeft) 176 elif index == 1: 177 self.alignmentLineEdit.setAlignment(Qt.AlignCenter) 178 elif index == 2: 179 self.alignmentLineEdit.setAlignment(Qt.AlignRight) 180 181 def inputMaskChanged(self, index): 182 if index == 0: 183 self.inputMaskLineEdit.setInputMask('') 184 elif index == 1: 185 self.inputMaskLineEdit.setInputMask('+99 99 99 99 99;_') 186 elif index == 2: 187 self.inputMaskLineEdit.setInputMask('0000-00-00') 188 self.inputMaskLineEdit.setText('00000000') 189 self.inputMaskLineEdit.setCursorPosition(0) 190 elif index == 3: 191 self.inputMaskLineEdit.setInputMask('>AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#') 192 193 def accessChanged(self, index): 194 if index == 0: 195 self.accessLineEdit.setReadOnly(False) 196 elif index == 1: 197 self.accessLineEdit.setReadOnly(True) 198 199 200if __name__ == '__main__': 201 202 import sys 203 204 app = QApplication(sys.argv) 205 window = Window() 206 window.show() 207 sys.exit(app.exec_()) 208