How to Check if a QLineEdit is Empty in Python

Empty strings are falsey in Python
Heads up! You've already completed this tutorial.

A reader asked:

I just want to know, how do I check whether a QLineEdit is empty or not?

The QLineEdit class doesn't have an isEmpty() method which you can call to find out if the line edit is empty, but we don't need one! Instead we can get the current text using .text() and then check if the returned value is an empty string.

Checking QLineEdit Text with .text()

In the code below lineedit is our already created QLineEdit widget.

python
text = lineedit.text()
if text == '': # if the line edit is empty, .text() will return an empty string.
     # do something

Using Python's Falsey Empty Strings

We can simplify this further. In Python empty strings are falsey -- they are considered False values in conditional expressions. So instead of checking the string is empty, we can check if it is true (non-empty) or false (empty).

python
if lineedit.text():
     # do something if there is content in the line edit.

Or, to check if it is empty:

python
if not lineedit.text():
     # do something if the line edit is empty.

Complete Example: Detecting Empty QLineEdit with Signals

Below is a small demo application which updates a label to indicate if the QLineEdit has text in it or not. In this we use Qt signals to send the current text to a slot method every time it is updated.

python
import sys

from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QVBoxLayout, QWidget


class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.lineedit = QLineEdit()
        self.lineedit.textChanged.connect(self.text_changed)

        self.label = QLabel()

        vlayout = QVBoxLayout()
        vlayout.addWidget(self.lineedit)
        vlayout.addWidget(self.label)

        self.setLayout(vlayout)

    def text_changed(self, s):

        # s contains the text of the line edit, we could also test self.lineedit.text()

        if s:
            self.label.setText("Not empty")

        else:
            self.label.setText("Empty")


app = QApplication(sys.argv)

w = Window()
w.show()

app.exec_()
python
import sys

from PyQt6.QtWidgets import QApplication, QLabel, QLineEdit, QVBoxLayout, QWidget


class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.lineedit = QLineEdit()
        self.lineedit.textChanged.connect(self.text_changed)

        self.label = QLabel()

        vlayout = QVBoxLayout()
        vlayout.addWidget(self.lineedit)
        vlayout.addWidget(self.label)

        self.setLayout(vlayout)

    def text_changed(self, s):

        # s contains the text of the line edit

        if s:
            self.label.setText("Not empty")

        else:
            self.label.setText("Empty")


app = QApplication(sys.argv)

w = Window()
w.show()

app.exec()

python
import sys

from PySide2.QtWidgets import QApplication, QLabel, QLineEdit, QVBoxLayout, QWidget


class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.lineedit = QLineEdit()
        self.lineedit.textChanged.connect(self.text_changed)

        self.label = QLabel()

        vlayout = QVBoxLayout()
        vlayout.addWidget(self.lineedit)
        vlayout.addWidget(self.label)

        self.setLayout(vlayout)

    def text_changed(self, s):

        # s contains the text of the line edit

        if s:
            self.label.setText("Not empty")

        else:
            self.label.setText("Empty")


app = QApplication(sys.argv)

w = Window()
w.show()

app.exec_()


python
import sys

from PySide6.QtWidgets import QApplication, QLabel, QLineEdit, QVBoxLayout, QWidget


class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.lineedit = QLineEdit()
        self.lineedit.textChanged.connect(self.text_changed)

        self.label = QLabel()

        vlayout = QVBoxLayout()
        vlayout.addWidget(self.lineedit)
        vlayout.addWidget(self.label)

        self.setLayout(vlayout)

    def text_changed(self, s):

        # s contains the text of the line edit

        if s:
            self.label.setText("Not empty")

        else:
            self.label.setText("Empty")


app = QApplication(sys.argv)

w = Window()
w.show()

app.exec_()


Run the above and you'll see the label update as you add and remove text in the QLineEdit.

Empty QLineEdit widget in PyQt/PySide

QLineEdit with content in PyQt/PySide

PyQt/PySide Development Services — Stuck in development hell? I'll help you get your project focused, finished and released. Benefit from years of practical experience releasing software with Python.

Find out More

This approach works across all Python Qt bindings including PyQt5, PyQt6, PySide2 and PySide6. By leveraging Python's truthiness checks on strings, you can validate QLineEdit input cleanly without needing a dedicated isEmpty() method. For more advanced input validation techniques, you may also want to look at input validation in Tkinter or explore the full range of PyQt6 widgets available for building your applications.

Over 15,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt6
Get the book

Downloadable ebook (PDF, ePub) & Complete Source code

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak
Martin Fitzpatrick

How to Check if a QLineEdit is Empty in Python was written by Martin Fitzpatrick.

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular Python books on the subject.