Course Content
Python PySide6
About Lesson

In this lesson we want to learn about Python PySide6 QLineEdit, PySide6 QLineEdit widget is used to create single-line text input field in graphical user interface. It is commonly used widget in PySide6 and allows users to enter and edit text in an application. In this lesson we will discuss how to work with PySide6 QLineEdit widget to create text input fields and customize their behavior.


Creating a QLineEdit

To create a new PySide6 QLineEdit in Python, we first need to import the QtWidgets module:



We can then create a new QLineEdit object by calling its constructor:

This creates a new QLineEdit object that can be added to our application’s layout using the addWidget() method of a layout object. For example, if we have a QVBoxLayout object called layout, we can add the line edit to it like this:


We can also set the initial text of the line edit using the setText() method:

This sets the text of the line edit to “Initial text”.


Retrieving Text

We can retrieve the text entered into the line edit by calling its text() method:

This returns the current text in the line edit.


Handling User Input

We can also customize the behavior of the line edit to respond to user input. For example, we can connect a function to the textChanged signal of the line edit that will be called whenever the text is changed:

This connects the handle_text_changed() function to the textChanged signal of the line edit. Whenever the user types in the line edit and the text is changed, the function will be called with the new text as an argument.


Validating User Input

We can also use the line edit to validate user input and ensure that it meets certain requirements. For example, we can set a regular expression that the text must match using the setValidator() method:

This sets a regular expression validator on the line edit that ensures that the text contains exactly three digits. If the user types in anything else, the line edit will not accept the input.



This is the GUI complete code for this.

In the above code, we have imported the QRegularExpression and QRegularExpressionValidator classes from PySide6.QtCore and PySide6.QtGui, respectively. 



Run the code and you can not write text in the field, you can just write numbers.

Python PySide6 QLineEdit
Python PySide6 QLineEdit




This is a complete example of using PySide6 to create a QLineEdit widget with some additional functionality:

In this example, we define new LineEditWidget class that inherits from QWidget. In the initUI method we create a QVBoxLayout layout and add a QLineEdit and QLabel widget to it. We also connect the returnPressed signal of the QLineEdit to the returnPressed method of LineEditWidget.

The returnPressed method gets text from the QLineEdit, sets it on the QLabel, and updates text of the label to reflect the text entered in the QLineEdit.

In main block of the code, we create a new LineEditWidget, set its window title, show it, and run the event loop.



Run the complete code write something in the QLineEdit and press enter.

Python PySide6 QLineEdit
Python PySide6 QLineEdit



Final Thoughts

PySide6 QLineEdit widget is a powerful tool for creating text input fields in graphical user interfaces. We can create new line edits, set their initial text, retrieve the text entered by the user, customize their behavior in response to user input, and validate user input to ensure that it meets certain requirements. With these tools, we can create informative and user-friendly graphical user interfaces that make it easy for users to enter and edit text in our applications.