In this PyQt6 lesson we are going to learn How to Create QLineEdit in PyQt6, we want to create our QLineEdit using Qt Designer.
What is QLineEdit in PyQt6 ?
QLineEdit is a widget in PyQt6 that allows the user to enter and edit single line of text. it is commonly used in forms, search boxes, and other applications where the user needs to provide a small amount of text input.
QLineEdit can be customized in different ways, including setting the text that appears initially, providing a placeholder text that disappears when the user starts typing, restricting the input to certain types of characters or formats, and connecting signals to respond to user input events.
First of all open your Qt Designer using pyqt5designer
- Add a QLineEdit from widget box
- Add QLabel and QPushButton, add QHBoxLayout for the Label and Button
- Set the main window layout as QVBoxLayout
Save your file, and as you know it is a .ui file, we need to convert this ui file to python file using pyuic6.
1 |
pyuic6 -x Linedit.ui -o mylinedit.py |
This is the converted file, also we have added signal and slot functionality for our button.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
from PyQt6 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(369, 270) self.verticalLayout_2 = QtWidgets.QVBoxLayout(Form) self.verticalLayout_2.setObjectName("verticalLayout_2") self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") self.lineEdit = QtWidgets.QLineEdit(Form) font = QtGui.QFont() font.setPointSize(14) self.lineEdit.setFont(font) self.lineEdit.setObjectName("lineEdit") self.verticalLayout.addWidget(self.lineEdit) self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.label = QtWidgets.QLabel(Form) font = QtGui.QFont() font.setPointSize(14) self.label.setFont(font) self.label.setText("") self.label.setObjectName("label") self.horizontalLayout.addWidget(self.label) self.pushButton = QtWidgets.QPushButton(Form) font = QtGui.QFont() font.setPointSize(12) self.pushButton.setFont(font) self.pushButton.setObjectName("pushButton") #We have connected the signal with the method self.pushButton.clicked.connect(self.btn_clicked) self.horizontalLayout.addWidget(self.pushButton) self.verticalLayout.addLayout(self.horizontalLayout) self.verticalLayout_2.addLayout(self.verticalLayout) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def btn_clicked(self): inputText = self.lineEdit.text() self.label.setText(inputText) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton.setText(_translate("Form", "Click")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec()) |
This is our method, in this method first we get the value from the lineedit, after that we set that with the label.
1 2 3 |
def btn_clicked(self): inputText = self.lineEdit.text() self.label.setText(inputText) |
In here we have connected the clicked signal of the QPushButton with our method, so when the user clicks the button, we want to change the text of the label with the input value.
1 |
self.pushButton.clicked.connect(self.btn_clicked) |
Run the complete code and this is the result.