Qt designer python lineedit

PyQt5 — QLineEdit Widget

QLineEdit object is the most commonly used input field. It provides a box in which one line of text can be entered. In order to enter multi-line text, QTextEdit object is required.

The following table lists a few important methods of QLineEdit class −

Aligns the text as per alignment constants

Controls the appearance of the text inside the box. Echomode values are −

Sets the maximum number of characters for input

Makes the text box non-editable

Programmatically sets the text

Retrieves text in the field

Sets the validation rules. Available validators are

QIntValidator − Restricts input to integer

QDoubleValidator − Fraction part of number limited to specified decimals

QRegexpValidator − Checks input against a Regex expression

Applies mask of combination of characters for input

Displays the contents QFont object

QLineEdit object emits the following signals −

Given below are the most commonly used methods of signals.

cursorPositionChanged()

Whenever the cursor moves

When you press ‘Enter’ or the field loses focus

Whenever the selected text changes

As text in the box changes either by input or by programmatic means

Whenever the text is edited

Example

QLineEdit objects in this example demonstrate use of some of these methods.

First field e1 shows text using a custom font, in right alignment and allows integer input. Second field restricts input to a number with 2 digits after decimal point. An input mask for entering the phone number is applied on the third field. textChanged() signal on the field e4 is connected to textchanged() slot method.

Contents of e5 field are echoed in password form as its EchoMode property is set to Password. Its editingfinished() signal is connected to presenter() method. So, once the user presses the Enter key, the function will be executed. The field e6 shows a default text, which cannot be edited as it is set to read only.

import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * def window(): app = QApplication(sys.argv) win = QWidget() e1 = QLineEdit() e1.setValidator(QIntValidator()) e1.setMaxLength(4) e1.setAlignment(Qt.AlignRight) e1.setFont(QFont("Arial",20)) e2 = QLineEdit() e2.setValidator(QDoubleValidator(0.99,99.99,2)) flo = QFormLayout() flo.addRow("integer validator", e1) flo.addRow("Double validator",e2) e3 = QLineEdit() e3.setInputMask('+99_9999_999999') flo.addRow("Input Mask",e3) e4 = QLineEdit() e4.textChanged.connect(textchanged) flo.addRow("Text changed",e4) e5 = QLineEdit() e5.setEchoMode(QLineEdit.Password) flo.addRow("Password",e5) e6 = QLineEdit("Hello Python") e6.setReadOnly(True) flo.addRow("Read Only",e6) e5.editingFinished.connect(enterPress) win.setLayout(flo) win.setWindowTitle("PyQt") win.show() sys.exit(app.exec_()) def textchanged(text): print "contents of text box: "+text def enterPress(): print "edited" if __name__ == '__main__': window()

Output

The above code produces the following output −

QLineEdit Widget Output

contents of text box: h contents of text box: he contents of text box: hel contents of text box: hell contents of text box: hello editing finished

Источник

PyQt QLineEdit

Summary: in this tutorial, you’ll learn how to use the PyQt QLineEdit widget to create a single-line text-entry widget.

Introduction to the PyQt QLineEdit widget

The PyQt QLineEdit allows you to create a single-line text-entry widget. Typically, you’ll use the QLineEdit in a data-entry form.

In practice, you often use the QLineEdit widget with a QLabel widget.

To create a QLineEdit widget, you follow these steps.

First, import QLineEdit from PyQt6.QtWidgets module:

from PyQt6.QtWidgets import QLineEditCode language: Python (python)

Second, create a new QLineEdit object that uses:

  • No arguments.
  • With only a parent widget.
  • Or with a default string value as the first argument.
line_edit = QLineEdit('Default Value', self)Code language: Python (python)

Also, you can use the following additional properties:

Property Type Description
text string The content of the line edit
readOnly Boolean True or False. If True, the line edit cannot be edited
clearButtonEnabled Boolean True to add a clear button
placeholderText string The text that appears when the line edit is empty
maxLength integer Specify the maximum number of characters that can be entered
echoMode QLineEdit .EchoMode Change the way the text displays e.g., password

PyQt QLineEdit widget examples

Let’s take some examples of using the QLineEdit widget.

1) Simple PyQt QLineEdit example

The following program shows how to create a QLineEdit widget:

import sys from PyQt6.QtWidgets import ( QApplication, QWidget, QLineEdit, QVBoxLayout ) class MainWindow(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('PyQt QLineEdit Widget') self.setGeometry(100, 100, 320, 210) search_box = QLineEdit( self, placeholderText='Enter a keyword to search. ', clearButtonEnabled=True ) # place the widget on the window layout = QVBoxLayout() layout.addWidget(search_box) self.setLayout(layout) # show the window self.show() if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec())Code language: Python (python)

2) Using the PyQt QLineEdit to create a password entry

The following program creates a new QLineEdit widget as a password entry:

import sys from PyQt6.QtWidgets import ( QApplication, QWidget, QLineEdit, QVBoxLayout ) class MainWindow(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('PyQt QLineEdit Widget') self.setGeometry(100, 100, 320, 210) password = QLineEdit(self, echoMode=QLineEdit.EchoMode.Password) # place the widget on the window layout = QVBoxLayout() layout.addWidget(password) self.setLayout(layout) # show the window self.show() if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec())Code language: Python (python)

To make the QLineEdit widget a password entry, you set the echoMode to QLineEdit . EchoMode.Password :

password = QLineEdit(self, echoMode=QLineEdit.EchoMode.Password)Code language: Python (python)

3) Using the PyQt QLineEdit with the auto-complete feature

To create an entry with the auto-complete feature, you follow these steps:

First, import the QCompleter from PyQt6.QtWidgets module.

Second, create a QCompleter widget with a list of strings used for autocomplete feature:

completer = QCompleter(word_list)Code language: Python (python)

Third, create a QLineEdit and call its setCompleter() method with the completer object:

line_edit = QLineEdit(self) line_edit.setCompleter(completer)Code language: Python (python)

For example, the following program shows a QLineEdit widget with an auto-complete feature:

import sys from PyQt6.QtWidgets import ( QApplication, QWidget, QLineEdit, QVBoxLayout, QCompleter ) class MainWindow(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('PyQt QLineEdit Widget') self.setGeometry(100, 100, 320, 210) common_fruits = QCompleter([ 'Apple', 'Apricot', 'Banana', 'Carambola', 'Olive', 'Oranges', 'Papaya', 'Peach', 'Pineapple', 'Pomegranate', 'Rambutan', 'Ramphal', 'Raspberries', 'Rose apple', 'Starfruit', 'Strawberries', 'Water apple', ]) fruit = QLineEdit(self) fruit.setCompleter(common_fruits) # place the widget on the window layout = QVBoxLayout() layout.addWidget(fruit) self.setLayout(layout) # show the window self.show() if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec())Code language: Python (python)

Summary

  • Use the QLineEdit to create a single-line entry widget.
  • Use the echoMode property to change the way the text is displayed.
  • Use the QLineEdit widget with a QCompleter widget to support the auto-complete feature.

Источник

QLineEdit#

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description#

../../_images/windows-lineedit.png

A line edit allows the user to enter and edit a single line of plain text with a useful collection of editing functions, including undo and redo, cut and paste, and drag and drop (see setDragEnabled() ).

By changing the echoMode() of a line edit, it can also be used as a “write-only” field, for inputs such as passwords.

The length of the text can be constrained to maxLength() . The text can be arbitrarily constrained using a validator() or an inputMask() , or both. When switching between a validator and an input mask on the same line edit, it is best to clear the validator or input mask to prevent undefined behavior.

A related class is QTextEdit which allows multi-line, rich text editing.

You can change the text with setText() or insert() . The text is retrieved with text() ; the displayed text (which may be different, see EchoMode ) is retrieved with displayText() . Text can be selected with setSelection() or selectAll() , and the selection can be cut() , copy() ied and paste() d. The text can be aligned with setAlignment() .

When the text changes the textChanged() signal is emitted; when the text changes other than by calling setText() the textEdited() signal is emitted; when the cursor is moved the cursorPositionChanged() signal is emitted; and when the Return or Enter key is pressed the returnPressed() signal is emitted.

When editing is finished, either because the line edit lost focus or Return/Enter is pressed the editingFinished() signal is emitted. Note that if focus is lost without any changes done, the editingFinished() signal won’t be emitted.

Note that if there is a validator set on the line edit, the returnPressed() / editingFinished() signals will only be emitted if the validator returns Acceptable .

By default, QLineEdits have a frame as specified by platform style guides; you can turn it off by calling setFrame (false).

The default key bindings are described below. The line edit also provides a context menu (usually invoked by a right mouse click) that presents some of these editing options. .. _desc:

Keypress

Action

Left Arrow

Moves the cursor one character to the left.

Shift+Left Arrow

Moves and selects text one character to the left.

Right Arrow

Moves the cursor one character to the right.

Shift+Right Arrow

Moves and selects text one character to the right.

Home

Moves the cursor to the beginning of the line.

End

Moves the cursor to the end of the line.

Backspace

Deletes the character to the left of the cursor.

Ctrl+Backspace

Deletes the word to the left of the cursor.

Delete

Deletes the character to the right of the cursor.

Ctrl+Delete

Deletes the word to the right of the cursor.

Ctrl+A

Select all.

Ctrl+C

Copies the selected text to the clipboard.

Ctrl+Insert

Copies the selected text to the clipboard.

Ctrl+K

Deletes to the end of the line.

Ctrl+V

Pastes the clipboard text into line edit.

Shift+Insert

Pastes the clipboard text into line edit.

Ctrl+X

Deletes the selected text and copies it to the clipboard.

Shift+Delete

Deletes the selected text and copies it to the clipboard.

Ctrl+Z

Undoes the last operation.

Ctrl+Y

Redoes the last undone operation.

Any other key sequence that represents a valid character, will cause the character to be inserted into the line edit.

Источник

Читайте также:  Java buffer file write
Оцените статью