pyqt5-QLineEdit

1、介绍

单行文本输入组件。

2、类和初始化

class QLineEdit(QWidget):
    """
    QLineEdit(parent: QWidget = None)
    QLineEdit(str, parent: QWidget = None)
    """
    def __init__(self, *__args):
        pass

快速整理:

QLineEdit(parent: typing.Optional[QWidget] = None)
QLineEdit(contents: str, parent: typing.Optional[QWidget] = None)
addAction(self, action: QAction)
addAction(self, action: QAction, position: QLineEdit.ActionPosition)
addAction(self, icon: QIcon, position: QLineEdit.ActionPosition) -> QAction
alignment(self) -> Qt.Alignment
backspace(self)
clear(self)
completer(self) -> QCompleter
copy(self)
createStandardContextMenu(self) -> QMenu
cursorBackward(self, mark: bool, steps: int = 1)
cursorForward(self, mark: bool, steps: int = 1)
cursorMoveStyle(self) -> Qt.CursorMoveStyle
cursorPosition(self) -> int
cursorPositionAt(self, pos: QPoint) -> int
cursorRect(self) -> QRect
cursorWordBackward(self, mark: bool)
cursorWordForward(self, mark: bool)
cut(self)
del_(self)
deselect(self)
displayText(self) -> str
dragEnabled(self) -> bool
echoMode(self) -> QLineEdit.EchoMode
end(self, mark: bool)
getTextMargins(self) -> Tuple[int, int, int, int]
hasAcceptableInput(self) -> bool
hasFrame(self) -> bool
hasSelectedText(self) -> bool
home(self, mark: bool)
initStyleOption(self, option: QStyleOptionFrame)
inputMask(self) -> str
inputMethodQuery(self, a0: Qt.InputMethodQuery) -> Any
inputMethodQuery(self, property: Qt.InputMethodQuery, argument: Any) -> Any
insert(self, a0: str)
isClearButtonEnabled(self) -> bool
isModified(self) -> bool
isReadOnly(self) -> bool
isRedoAvailable(self) -> bool
isUndoAvailable(self) -> bool
maxLength(self) -> int
minimumSizeHint(self) -> QSize
paste(self)
placeholderText(self) -> str
redo(self)
selectAll(self)
selectedText(self) -> str
selectionEnd(self) -> int
selectionLength(self) -> int
selectionStart(self) -> int
setAlignment(self, flag: Union[Qt.Alignment, Qt.AlignmentFlag])
setClearButtonEnabled(self, enable: bool)
setCompleter(self, completer: QCompleter)
setCursorMoveStyle(self, style: Qt.CursorMoveStyle)
setCursorPosition(self, a0: int)
setDragEnabled(self, b: bool)
setEchoMode(self, a0: QLineEdit.EchoMode)
setFrame(self, a0: bool)
setInputMask(self, inputMask: str)
setMaxLength(self, a0: int)
setModified(self, a0: bool)
setPlaceholderText(self, a0: str)
setReadOnly(self, a0: bool)
setSelection(self, a0: int, a1: int)
setText(self, a0: str)
setTextMargins(self, left: int, top: int, right: int, bottom: int)
setTextMargins(self, margins: QMargins)
setValidator(self, a0: QValidator)
sizeHint(self) -> QSize
text(self) -> str
textMargins(self) -> QMargins
undo(self)
validator(self) -> QValidator
---------------
changeEvent(self, a0: QEvent)
contextMenuEvent(self, a0: QContextMenuEvent)
dragEnterEvent(self, a0: QDragEnterEvent)
dragLeaveEvent(self, e: QDragLeaveEvent)
dragMoveEvent(self, e: QDragMoveEvent)
dropEvent(self, a0: QDropEvent)
event(self, a0: QEvent) -> bool
focusInEvent(self, a0: QFocusEvent)
focusOutEvent(self, a0: QFocusEvent)
inputMethodEvent(self, a0: QInputMethodEvent)
keyPressEvent(self, a0: QKeyEvent)
mouseDoubleClickEvent(self, a0: QMouseEvent)
mouseMoveEvent(self, a0: QMouseEvent)
mousePressEvent(self, a0: QMouseEvent)
mouseReleaseEvent(self, a0: QMouseEvent)
paintEvent(self, a0: QPaintEvent)
---------------
cursorPositionChanged(self, a0: int, a1: int) [signal]
editingFinished(self) [signal]
inputRejected(self) [signal]
returnPressed(self) [signal]
selectionChanged(self) [signal]
textChanged(self, a0: str) [signal]
textEdited(self, a0: str) [signal]

3、属性

    LeadingPosition = 0
    NoEcho = 1
    Normal = 0
    Password = 2
    PasswordEchoOnEdit = 3
    TrailingPosition = 1

4、方法

(1)setText

  • 设置文本,相当于用户直接输入框输入,参数类型为str

(2)text

  • 获取文本,即用户输入的,返回类型str

(3)setReadOnly

  • 设置输入框是否只读,即不允许编辑。参数类型为bool
  • 当设置为只读,将无法选中输入框

(4)isReadOnly

  • 获取是否只读,返回类型bool

(5)setPlaceholderText

  • 设置占位符提示文本,参数类型是str
  • 其作用是当文本框输入文本为空时,显示用于提示

(6)placeholderText

  • 获取占位符提示文本,返回类型str

(7)clear

  • 将文本框中所有的输入文本内容清除

(7)copy

  • 复制选中的内容,如果没有选中内容,则不进行复制。剪切板中仍然是上一复制的内容
  • 注意,当鼠标点击输入框外的组件时,丢失选中内容,无法赋值。这时如果想赋值,必须使用方法setSelection或selectAll进行先选中,再操作

(8)cut

  • 剪切选中的内容,如果没有选中内容,则不进行剪切。剪切板中仍然是上一复制的内容

(9)paste

  • 粘贴。无参无返回
  • 作用是:鼠标在文本框中的光标处插入剪切板中存储的文本。即使该文本框未聚焦,也会有一个光标位置可以使用,默认是末尾处

(10)selectedText

  • 获取选中文本,返回类型str,如果没有选中返回空字符串””
  • 注意当前文本框必须聚焦,如果脱离点击了其他组件,选中必然是””

(11)setSelection

  • 设置选中范围,两个参数都是int类型,分别表示起止索引,从0开始,超出不会报错,而是包含末端在内

(12)selectionEnd

  • 被选中部分的结束索引,没有选中时,返回-1

(13)selectionLength

  • 被选中部分的长度,是实际长度,如果没有选中,返回0

(14)selectionStart

  • 被选中部分的开始索引,没有选中时,返回-1

(15)selectAll

  • 选中全部

(16)deselect

  • 取消选中

(17)hasSelectedText

  • 判断是否有选中,返回bool

(18)insert

  • 在光标处插入文本,参数类型为str

(19)setCursorPosition

  • 设置光标的位置,参数类型为int
  • 一般会在前或后使用setFocus方法聚焦,显示光标位置。当然,不显示,也是会移动的
  • 参数范围是0-len,0表示光标在最开始部分。len表示在最末尾部分。1表示光标在索引为1的字符之前。
  • 参数超出表达范围,不会进行移动光标。之前是在哪就在哪。

(20)setFocus

  • 使输入框聚焦,可以显示光标位置。该方法是QWidget类的
  • 默认的话,鼠标点击其他组件,会使输入框组件丢失焦点,光标不再显示。此时光标是有效的。但如果再次点击输入框,光标会根据点击再次确认,也就是说无法观察中间过程的光标。

(21)cursorPosition

  • 获取光标位置,int类型
  • 参照上面设置时的解释,范围是0-len

(22)setCursorMoveStyle

  • 设置光标移动的样式
setCursorMoveStyle(self, Qt.CursorMoveStyle)

5、事件

selectionChanged(self) [signal]	# 选中部分发生改变
textChanged(self, str) [signal] # 内容发生改变
returnPressed(self) [signal]	# 回车键按下
cursorPositionChanged(self, int, int) [signal]# 光标位置发生改变,改变前后的索引

 

posted @ 2023-04-16 23:40  挖洞404  阅读(61)  评论(0编辑  收藏  举报