A Line Edit is a single line editor of plain text. It is used to create blank or pre-filled input boxes, the most classical example would be the input and output screen of a calculator.
The maximum text length is set to 32767 characters.
When creating a LineEdit box, you can choose whether or not input strings into the box:
strings in the box will show as placeholder in our final window:
these placeholder strings can be deleted when input.
editing functions can be used in line edit including:
Obtain the value of LineEdit
Normally, if our app asks users to input something, it means our app will use the information (strings or values) to implement some functions. So our next step after creating a LineEdit ui element would be obtaining the input value.
create your ui
Let's see a simple example. We are going to create a window with a LineEdit box, a PushButton and a Label in Qt Designer, the Label will be used to capture and show the value imputed in the LineEdit box.
Alignment: ①Horizontal layout for
Inputed valuelabel; ② Vertical layout for result of the step ① and the Line Edit box; ③right click at empty area of the form window and apply Vertical layout to all the elements.
Save the ui file to our project folder and rename it to
lineedit.ui, you can of course call it whatever you want.
Convert the ui file to python script by command:
pyuic6 -x lineedit.ui -o runlineedit.py
Note the command should be executed in exactly in the folder where you ui file exists.
runlineedit.pywith your own names.
now we can run the window the see how it looks like:
runlineedit.py we are going to write of new method and connect it tow the button:
def setupUi(self, Form) ... self.pushButton.clicked.connect(click_to_get_and_show) # connect to new method def click_to_get_and_show(self): InputValue = self.lineEdit.text() # get input value self.label.setText(InputValue) # set the label to be input value def retranslateUi(self, Form): ...
text() function is used to get the value of the input box, it returns strings.
now save and run this file again:
interesting right? This window could be a prototype of a calculator!
Since the the
text() returns strings, some conversion would be made if the value is going to be used for calculating.
EchoMode of LineEdit
EchoMode is how texts (characters) are displayed while users are typing,. By default, the EhcoMode is set to
Normal, normal means show everything. However, if a line edit is designed to accept a password, you may consider resetting its EchoMode to a "password type".
There are four echo modes in total:
Normal: display what you input
NoEcho: display nothing
Password: display symbol of black circle
PasswordEchoOnEdit: display characters as they are entered while editing otherwise display characters as symbols.
PasswordEchoOnEdit are all "password type" modes.
Let's try to change the EchoMode of the former example, in our generated
runlineedit.py file, find lines of
lineEdit and add:
class Ui_Form(object): def setupUi(self, Form): ... self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setObjectName("lineEdit") self.lineEdit.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password) # set the EchoMode
you can replace the
Password with other two types of password-like modes to see the how they look like.
save and run: