pyqt4文档阅读(5):QLineEdit

本系列文章长期更新修改.


QLineEdit,最基础的单行输入框.



属性:

Types

  • enum EchoMode { Normal, NoEcho, Password, PasswordEchoOnEdit }

Methods

  • __init__ (self, QWidget parent = None)
  • __init__ (self, QString, QWidget parent = None)
  • Qt.Alignment alignment (self)
  • backspace (self)
  • changeEvent (self, QEvent)
  • clear (self)
  • QCompleter completer (self)
  • contextMenuEvent (self, QContextMenuEvent)
  • copy (self)
  • QMenu createStandardContextMenu (self)
  • cursorBackward (self, bool mark, int steps = 1)
  • cursorForward (self, bool mark, int steps = 1)
  • int cursorPosition (self)
  • int cursorPositionAt (self, QPoint pos)
  • QRect cursorRect (self)
  • cursorWordBackward (self, bool mark)
  • cursorWordForward (self, bool mark)
  • cut (self)
  • del_ (self)
  • deselect (self)
  • QString displayText (self)
  • bool dragEnabled (self)
  • dragEnterEvent (self, QDragEnterEvent)
  • dragLeaveEvent (self, QDragLeaveEvent e)
  • dragMoveEvent (self, QDragMoveEvent e)
  • dropEvent (self, QDropEvent)
  • EchoMode echoMode (self)
  • end (self, bool mark)
  • bool event (self, QEvent)
  • focusInEvent (self, QFocusEvent)
  • focusOutEvent (self, QFocusEvent)
  • bool hasAcceptableInput (self)
  • bool hasFrame (self)
  • bool hasSelectedText (self)
  • home (self, bool mark)
  • initStyleOption (self, QStyleOptionFrame option)
  • QString inputMask (self)
  • inputMethodEvent (self, QInputMethodEvent)
  • QVariant inputMethodQuery (self, Qt.InputMethodQuery)
  • insert (self, QString)
  • bool isModified (self)
  • bool isReadOnly (self)
  • bool isRedoAvailable (self)
  • bool isUndoAvailable (self)
  • keyPressEvent (self, QKeyEvent)
  • int maxLength (self)
  • QSize minimumSizeHint (self)
  • mouseDoubleClickEvent (self, QMouseEvent)
  • mouseMoveEvent (self, QMouseEvent)
  • mousePressEvent (self, QMouseEvent)
  • mouseReleaseEvent (self, QMouseEvent)
  • paintEvent (self, QPaintEvent)
  • paste (self)
  • redo (self)
  • selectAll (self)
  • QString selectedText (self)
  • int selectionStart (self)
  • setAlignment (self, Qt.Alignment flag)
  • setCompleter (self, QCompleter completer)
  • setCursorPosition (self, int)
  • setDragEnabled (self, bool b)
  • setEchoMode (self, EchoMode)
  • setFrame (self, bool)
  • setInputMask (self, QString inputMask)
  • setMaxLength (self, int)
  • setModified (self, bool)
  • setReadOnly (self, bool)
  • setSelection (self, int, int)
  • setText (self, QString)
  • setValidator (self, QValidator)
  • QSize sizeHint (self)
  • QString text (self)
  • undo (self)
  • QValidator validator (self)

Qt Signals

  • void cursorPositionChanged (int,int)
  • void editingFinished ()
  • void returnPressed ()
  • void selectionChanged ()
  • void textChanged (const QString&)
  • void textEdited (const QString&)

详细分析:


1.显示模式
QLineEdit有四种显示模式,可通过下面函数设置:
  • EchoMode echoMode (self)
  • setEchoMode (self, EchoMode)
  • enum EchoMode { Normal, NoEcho, Password, PasswordEchoOnEdit }
ConstantValue描述
QLineEdit.Normal0默认模式,正常显示
QLineEdit.NoEcho1完全不显示输入,部分操作被禁止
QLineEdit.Password2密码模式,像输入密码那样,把输入内容变成一个个圆圈
QLineEdit.PasswordEchoOnEdit3输入时显示内容,失去焦点时内容变成圆圈
2.属性,限制和状态的相关函数(打*号为很少用)
function属性描述
__init__ (self, QWidget parent = None)
__init__ (self, QString, QWidget parent = None)
初始化第二个init能设置默认内容
QString text (self)
setText (self, QString)
内容 
QString displayText (self)*获取显示的内容与text()的主要区别是当QLineEdit是密码模式的时候,这个函数得到是圆圈
int maxLength (self)
setMaxLength (self, int)
输入文本的长度限制默认值是32767
bool isReadOnly (self)
setReadOnly (self, bool)
是否只读默认是false
bool isModified (self)
setModified (self, bool)
内容是否被修改过脏位,setText()不会影响这个属性
bool dragEnabled (self)
setDragEnabled (self, bool b)
内容是否能被拖动默认是false
int cursorPosition (self)
setCursorPosition (self, int)
光标位置 
QRect cursorRect (self)*光标位置+宽高这个位置是坐标位置,不是文本位置
int cursorPositionAt (self, QPoint pos)*坐标位置转文本位置 
int selectionStart (self)被选择文本的开头位置没有选择文本的时候,返回-1
QString selectedText (self)被选择的文本 
bool hasSelectedText (self)是否选择了文本 
bool isRedoAvailable (self)*当前是否能重做 
bool isUndoAvailable (self)*当前是否能撤销 
3.键盘特殊输入与模拟操作
QLineEdit支持常用的文本快捷键如复制粘贴等,并提供了一套函数模拟这些操作.
Keypress描述模拟函数
Left Arrow光标向左移动cursorBackward (self, bool mark, int steps = 1)
Shift+Left Arrow光标向左移动,同时选中文本cursorBackward (self, bool mark, int steps = 1)
Right Arrow光标向右移动cursorForward (self, bool mark, int steps = 1)
Shift+Right Arrow光标向左移动,同时选中文本cursorForward (self, bool mark, int steps = 1)
Home光标移到最始端home (self, bool mark)
End光标移到最末端end (self, bool mark)
Backspace在光标左侧删除一个字符或选中的文本backspace (self)
Ctrl+Backspace在光标左侧删除一个词或选中的文本
Delete在光标右侧删除一个字符或选中的文本del_ (self)
Ctrl+Delete在光标右侧删除一个词或选中的文本
Ctrl+A全选selectAll (self)
Ctrl+C复制copy (self)
Ctrl+Insert复制copy (self)
Ctrl+K**
Ctrl+V粘贴paste (self)
Shift+Insert粘贴paste (self)
Ctrl+X剪切cut (self)
Shift+Delete剪切cut (self)
Ctrl+Z撤销undo (self)
Ctrl+Y重做redo (self)
光标向左移动一个词cursorWordBackward (self, bool mark)
光标向右移动一个词cursorWordForward (self, bool mark)
取消选择deselect (self)
清空内容clear (self)
插入一个字符串insert (self, QString)
选择一个子串(beg,len)setSelection (self, int, int)
1.所有移动的操作函数里都带有mark,mark为true代表移动时要顺便选中文本.只要移动中一出现false,马上取消选择.
2.Ctrl+K是删除光标右侧该行的所有字符,在windows下无效
4.信号
  1. void cursorPositionChanged (int,int)
    • 光标移动的时候激活
    • 两个参数分别为旧坐标和新坐标
    • 输入过程中光标也算移动
  2. void editingFinished ()
    • 失去焦点或者键入Enter后激活
    • 设置了validator()验证函数或者inputMask()格式函数时,必须正确验证或符合格式才激活
  3. void returnPressed ()
    • 键入Enter后激活
    • 设置了validator()验证函数或者inputMask()格式函数时,必须正确验证或符合格式才激活
  4. void selectionChanged ()
    • 选择的文本改变的时候激活
    • 用鼠标选择文本的时候,只要鼠标移动信号就会激活
  5. void textChanged (const QString&)
    • 文本改变时激活
    • 这种改变包括使用setText()这样的函数
  6. void textEdited (const QString&)
    • 文本改变时激活
    • 这种改变不包括使用setText()这样的函数
5.输入检查

QLineEdit有两种手段对输入进行合法性检查,validator和inputMask.

  • QValidator validator (self)
  • QString inputMask (self)
  • setValidator (self, QValidator)
  • setInputMask (self, QString inputMask)
validator是Qt专门用来对输入进行合法性检查的一个类QValidator,详细用法参考QValidator.
inputMask是Qt的一个小型格式字符串,用法有点像正则表达式,但功能弱很多,详细参考inputMask.
想知道当前文本是否合法,可以调用下面函数:
  • bool hasAcceptableInput (self)
注意一点,只有检查合法的情况下,上文提到的某些信号才会激活.
6.自动补全
QLineEdit能通过类QCompleter实现自动补全,详细用法参考QCompleter.
  • QCompleter completer (self)
  • setCompleter (self, QCompleter completer)
7.外边框
可以通过下面相关函数设置QLineEdit有或没有外边框,true表示有,默认有.
  • bool hasFrame (self)
  • setFrame (self, bool)
8.对齐设置
下面相关函数可以对QLineEdit的文字如何对齐进行设置.
  • Qt.Alignment alignment (self)
  • setAlignment (self, Qt.Alignment flag)
设置的时候使用Qt.AlignmentFlag里面的枚举量即可,但获取的时候会得到一个Qt.Alignment对象,暂时不知道这个对象如何使用.
9.待续
QMenu createStandardContextMenu (self)
QVariant inputMethodQuery (self, Qt.InputMethodQuery)

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值