(9) 信号与槽函数 :
++
++
++
(10)以下源代码来自于头文件 qlineedit . h :
#ifndef QLINEEDIT_H
#define QLINEEDIT_H
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qframe.h>
#include <QtGui/qtextcursor.h>
#include <QtCore/qstring.h>
#include <QtCore/qmargins.h>
QT_REQUIRE_CONFIG(lineedit);
QT_BEGIN_NAMESPACE //说明本类定义于 Qt的全局命名空间
class QValidator;
class QMenu;
class QLineEditPrivate;
class QCompleter;
class QStyleOptionFrame;
class QAbstractSpinBox;
class QDateTimeEdit;
class QIcon;
class QToolButton;
/*
The QLineEdit widget is a one-line text editor.
A line edit 允许用户输入和编辑一行纯文本,
其中包含有用的编辑功能,包括撤消和重做、剪贴和拖放(参见setDragEnabled())。
通过更改line edit的echoMode()函数,它也可以用作“只写”字段,用于密码等输入。
文本的长度可以受到maxLength()的限制。
可以使用验证器()或输入掩码()或两者来任意限制文本。
当在同一行编辑中切换验证器和输入掩码时,最好清除验证器或输入掩码,以防止未定义行为。
您可以使用setText()或insert()方法更改文本。使用text()方法检索文本;
使用displayText方法检索显示的文本(可能不同,请参阅EchoMode)。
可以使用setSelection()或selectAll()选择文本,并且可以剪切,复制和粘贴选择。
可以使用setAlignment()对文本进行对齐。
当文本更改时,会发出textChanged()信号;
当通过调用setText()以外的方式更改文本时,会发出 textEdited()信号;
当光标移动时,会发出 cursorPositionChanged()信号;
当按下返回或 Enter键时,会发出 returnPressed()信号。
and when the Return or Enter key is pressed the returnPressed() signal is emitted.
当编辑完成时,无论是由于行编辑失去焦点还是按下“返回/Enter”键,
the editingFinished() signal is emitted.
请注意,如果在未进行任何更改的情况下失去焦点,则不会发出 editingFinished() signal。
请注意,如果行编辑上设置了验证器 validator,
则只有在验证器返回 QValidator::Acceptable 时才会发出returnPressed()/editingFinished()信号。
默认情况下,QLineEdits具有由平台样式指南指定的框架,您可以通过调用setFrame(false)来关闭它。
默认的键绑定如下所示。 The default key bindings are described below.
行编辑还提供了一个上下文菜单(通常通过右键单击调用),其中显示了这些编辑选项中的一些。
........................................ctrl + c, ctrl + v 这些的含义,表略
任何其他表示有效字符的关键序列都将导致该字符被插入到行编辑器中。
*/
class Q_WIDGETS_EXPORT QLineEdit : public QWidget
{
Q_OBJECT
//This property holds the line edit's text.
//Setting this property clears the selection, clears the undo/redo history,
//moves the cursor to the end of the line and resets the
// modified property to false. The text is not validated when inserted with
//setText(). The text is truncated to maxLength() length.
//By default, this property contains an empty string.
Q_PROPERTY(QString text READ text
WRITE setText NOTIFY textChanged USER true)
Q_PROPERTY(bool hasSelectedText READ hasSelectedText)
//This property holds whether there is any text selected.
//hasSelectedText() returns true if some or all of the
//text has been selected by the user; otherwise returns false.
//By default, this property is false.
//This property holds the selected text.
//If there is no selected text this property's value is an empty string.
//By default, this property contains an empty string.
Q_PROPERTY(QString selectedText READ selectedText)
//echo 回声;回波;回声 //本机的密码形式是大黑点而非* 号。
//This enum type describes how a line edit should display its contents.
Q_PROPERTY(EchoMode echoMode READ echoMode WRITE setEchoMode) //密码策略
//enum QLineEdit::EchoMode { Normal, NoEcho, Password, PasswordEchoOnEdit };
//This property holds the displayed text.
//If echoMode is Normal this returns the same as text();
//if EchoMode is Password or PasswordEchoOnEdit, it returns a string of
// platform-dependent password mask characters text().length() in size,
//e.g. "******"; if EchoMode is NoEcho returns an empty string, "".
//By default, this property contains an empty string.
Q_PROPERTY(QString displayText READ displayText) //密码策略影响后的文本
//此属性保存显示的文本。 如果echoMode是Normal,则返回与text()相同的结果;
//如果EchoMode是Password或PasswordEchoOnEdit,
// 则返回一个由平台依赖的密码掩码字符组成的字符串,大小与text().length()相同,例如“******”;
//如果EchoMode是NoEcho,则返回空字符串“”。 默认情况下,此属性包含一个空字符串。
Q_PROPERTY(QString placeholderText READ placeholderText //占位符文本
WRITE setPlaceholderText)
//此属性包含“行编辑”的占位符文本,
//设置此属性后,只要行编辑为空,行编辑就会显示一个灰色占位符文本。
//通常情况下,即使处于焦点状态,空行编辑也会显示占位符文本。
//但是,如果内容水平居中,当行编辑处于焦点状态时,占位符文本不会在光标下方显示。
//默认情况下,此属性包含一个空字符串。
//结论:占位符对于水平左对齐、右对齐都可正常工作,但对水平中心对齐不起作用。
//This property holds whether the line edit draws itself with a frame.
//If enabled (the default) the line edit draws itself inside a frame,
//otherwise the line edit draws itself without any frame.
Q_PROPERTY(bool frame READ hasFrame WRITE setFrame) //是否绘制边框
//This property holds the alignment of the line edit.
//Both horizontal and vertical alignment is allowed here,
//Qt::AlignJustify will map to Qt::AlignLeft.
//By default, this property contains a combination of
// Qt::AlignLeft and Qt::AlignVCenter. //内容文本的对齐方式
Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
//This property holds whether the line edit is read only.
//In read-only mode, the user can still copy the text to the clipboard,
//or drag and drop the text (if echoMode() is Normal), but cannot edit it.
//QLineEdit does not show a cursor in read-only mode.
//By default, this property is false. //本行文本框是否是只读的
Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly) //是否只读
//This property holds whether the line edit displays a clear button when
// it is not empty. If enabled, the line edit displays a trailing
//clear button when it contains some text,
//otherwise the line edit does not show a clear button (the default).
Q_PROPERTY(bool clearButtonEnabled //清除按钮是否可用可见
READ isClearButtonEnabled WRITE setClearButtonEnabled)
//This property holds the validation input mask.
//If no mask is set, inputMask() returns an empty string.
//Sets the QLineEdit's validation mask. Validators can be used instead of,
//or in conjunction with masks; see setValidator().
//Unset the mask and return to normal QLineEdit operation by passing an
//empty string (""). The input mask is an input template string.
//It can contain the following elements: Mask/Meta Characters、
//Mask Characters:Defines the Category类别 of input characters that
// are considered valid in this position。
//Meta Characters:Various special meanings。
//Separators :All other characters are regarded as immutable不可变 separators.
Q_PROPERTY(QString inputMask READ inputMask WRITE setInputMask) //输入掩码
//当创建或清除时,行编辑将填充输入掩码字符串的副本,
//其中元字符已被删除,掩码字符已被空白字符(默认为空格)替换。
//当设置输入掩码时,text()方法返回行编辑内容的修改副本,其中所有空白字符都被删除。
// 可以使用displayText()读取未修改的内容。
//如果行编辑器的当前内容不符合输入掩码的要求,则hasAcceptablelnput()方法返回false。
//To get range control (e.g., for an IP address) use masks together with validators.
Q_PROPERTY(bool acceptableInput READ hasAcceptableInput) //输入是否合规
//This property holds whether the input satisfies the inputMask and the validator.
//By default, this property is true.
Q_PROPERTY(int maxLength READ maxLength WRITE setMaxLength)
//此属性包含文本的最大允许长度。 如果文本太长,则会在极限处截断。
//如果发生截断,任何选中的文本都将被取消选择,光标位置设置为0,并显示字符串的第一部分。
//如果行编辑具有输入掩码,则掩码定义最大字符串长度。默认情况下,此属性包含值为 32767。
//此属性保存此行编辑器的当前光标位置。设置光标位置会在适当的时候引起重新绘制。
//默认情况下,此属性包含值为 0。
Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition)//光标位置
Q_PROPERTY(bool modified READ isModified WRITE setModified DESIGNABLE false)
//此属性表示是否由用户修改了“行编辑”的内容。 修改后的标志永远不会被 QLineEdit 读取;
//它具有默认值为 false 的值,并且每当用户更改QLineEdit 的内容时,它都会更改为 true。
//这对于需要提供默认值但不知道默认值应该是什么的情况很有用(也许它取决于表单上的其他字段)。
//在没有最佳默认值的情况下开始行编辑,
//当知道默认值时,如果modified()返回false(用户没有输入任何文本),则插入默认值。
//调用 setText()方法会将修改标志重置为 false。
//This property holds whether the lineedit starts a drag if the
// user presses and moves the mouse on some selected text.
//Dragging is disabled by default.
Q_PROPERTY(bool dragEnabled READ dragEnabled WRITE setDragEnabled) //默认不许拖动
//This property holds whether undo is available.
//Undo becomes available once the user has modified the text in the line edit.
//By default, this property is false.
Q_PROPERTY(bool undoAvailable READ isUndoAvailable) //有修改后才可以执行撤销
Q_PROPERTY(bool redoAvailable READ isRedoAvailable) //有撤销操作后才可以重做
//This property holds whether redo is available.
//Redo becomes available once the
//user has performed one or more undo operations on text in the line edit.
//By default, this property is false.
// enum Qt::CursorMoveStyle { LogicalMoveStyle, VisualMoveStyle };
Q_PROPERTY(Qt::CursorMoveStyle cursorMoveStyle //感觉没啥区别,下标 0始终在左边
READ cursorMoveStyle WRITE setCursorMoveStyle)
//此属性持有此行编辑中的光标移动样式。
//当此属性设置为 Ot::VisualMoveStyle 时,行编辑将使用视觉移动样式。
//按下左箭头键将始终使光标向左移动,而不管文本的书写方向如何。右箭头键也具有相同的行为。
//当属性为 Qt::LogicalMoveStyle(默认值)时,在 LTR 文本块内,
//按左箭头键时增加光标位置,按右箭头键时减少光标位置。如果文本块是右对齐的,则行为相反。
private:
friend class QAbstractSpinBox;
friend class QAccessibleLineEdit;
friend class QComboBox;
#ifdef QT_KEYPAD_NAVIGATION //经测试,无此定义
friend class QDateTimeEdit;
#endif
#ifdef QT_KEYPAD_NAVIGATION
Q_PRIVATE_SLOT(d_func(), void _q_editFocusChange(bool))
#endif
Q_PRIVATE_SLOT(d_func(), void _q_handleWindowActivate())
Q_PRIVATE_SLOT(d_func(), void _q_textEdited(const QString &))
Q_PRIVATE_SLOT(d_func(), void _q_cursorPositionChanged(int, int))
Q_PRIVATE_SLOT(d_func(), void _q_completionHighlighted(const QString &))
Q_PRIVATE_SLOT(d_func(), void _q_selectionChanged())
Q_PRIVATE_SLOT(d_func(), void _q_updateNeeded(const QRect &))
Q_PRIVATE_SLOT(d_func(), void _q_textChanged(const QString &))
Q_PRIVATE_SLOT(d_func(), void _q_clearButtonClicked())
Q_PRIVATE_SLOT(d_func(), void _q_controlEditingFinished())
Q_DISABLE_COPY(QLineEdit)
Q_DECLARE_PRIVATE(QLineEdit)
public:
//This enum type describes how a line edit should display the
// action widgets to be added. //Leading 前面,Trailing 后面
enum ActionPosition { //意思是新添加的控件是放前面还是放后面的意思
LeadingPosition, //The widget is displayed to the left of the
//text when using layout direction Qt::LeftToRight or to the
//right when using Qt::RightToLeft, respectively.
TrailingPosition //The widget is displayed to the right of the
//text when using layout direction Qt::LeftToRight or to the
//left when using Qt::RightToLeft, respectively.
};
Q_ENUM(ActionPosition)
//Constructs a line edit with no text.
//The maximum text length is set to 32767 characters.
//The parent argument is sent to the QWidget constructor.
explicit QLineEdit(QWidget * parent = nullptr);
explicit QLineEdit(const QString & contents, QWidget * parent = nullptr);
//Constructs a line edit containing the text contents.
//The cursor position is set to the end of the line and the
// maximum text length to 32767 characters.
//The parent and argument is sent to the QWidget constructor.
~QLineEdit();
//Q_PROPERTY(QString text READ text
// WRITE setText NOTIFY textChanged USER true)
QString text() const;
public Q_SLOTS:
void setText( const QString &);
Q_SIGNALS:
void textChanged(const QString &);
public:
//Q_PROPERTY(QString selectedText READ selectedText)
QString selectedText() const;
//Q_PROPERTY(bool hasSelectedText READ hasSelectedText)
bool hasSelectedText() const;
/*
Normal:Display characters as they are entered. This is the default.没有保密措施
NoEcho:Do not display anything. 不显示任何数据,包括文本的长度
This may be appropriate for passwords where even the
length of the password should be kept secret.
Password:Display platform-dependent password mask characters instead of the
characters actually entered. 使用系统的密码字符,* 或黑点,给出了文本的长度信息
PasswordEchoOnEdit:Display characters as they are entered while editing,
otherwise display characters as with Password.
//输入时为正常文本,失去焦点后,就变成密文了。按 enter不起作用。
*/
enum EchoMode { Normal, NoEcho, Password, PasswordEchoOnEdit };
Q_ENUM( EchoMode )
//Q_PROPERTY(EchoMode echoMode READ echoMode WRITE setEchoMode) //密码策略
EchoMode echoMode() const;
void setEchoMode(EchoMode);
//Q_PROPERTY(QString displayText READ displayText) //密码策略影响后的文本
QString displayText() const;
//Q_PROPERTY(QString placeholderText READ placeholderText //占位符文本
// WRITE setPlaceholderText)
QString placeholderText() const;
void setPlaceholderText(const QString &);
//Q_PROPERTY(bool frame READ hasFrame WRITE setFrame) //是否绘制边框
bool hasFrame() const;
void setFrame(bool);
//Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
Qt::Alignment alignment() const;
void setAlignment(Qt::Alignment flag);
//Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly) //是否只读
bool isReadOnly() const;
void setReadOnly(bool);
//Q_PROPERTY(bool clearButtonEnabled //清除按钮是否可用可见
// READ isClearButtonEnabled WRITE setClearButtonEnabled)
bool isClearButtonEnabled() const;
void setClearButtonEnabled(bool enable);
//Q_PROPERTY(QString inputMask READ inputMask WRITE setInputMask) //输入掩码
QString inputMask() const;
void setInputMask(const QString & inputMask);
//Q_PROPERTY(bool acceptableInput READ hasAcceptableInput) //输入是否合规
bool hasAcceptableInput() const;
//Q_PROPERTY(int maxLength READ maxLength WRITE setMaxLength)
int maxLength() const;
void setMaxLength(int);
//Q_PROPERTY(int cursorPosition
// READ cursorPosition WRITE setCursorPosition)//光标位置
int cursorPosition() const;
void setCursorPosition(int);
//Q_PROPERTY(bool modified
// READ isModified WRITE setModified DESIGNABLE false)
bool isModified() const;
void setModified(bool);
//Q_PROPERTY(bool dragEnabled
// READ dragEnabled WRITE setDragEnabled) //默认不许拖动
bool dragEnabled() const;
void setDragEnabled(bool b);
//Q_PROPERTY(bool undoAvailable READ isUndoAvailable) //有修改后才可以执行撤销
bool isUndoAvailable() const;
//Q_PROPERTY(bool redoAvailable READ isRedoAvailable) //有撤销操作后才可以重做
bool isRedoAvailable() const;
//Q_PROPERTY(Qt::CursorMoveStyle cursorMoveStyle //感觉没啥区别,下标 0始终在左边
// READ cursorMoveStyle WRITE setCursorMoveStyle)
Qt::CursorMoveStyle cursorMoveStyle() const;
void setCursorMoveStyle(Qt::CursorMoveStyle style);
const QValidator * validator() const;
void setValidator(const QValidator *);
QCompleter * completer() const;
void setCompleter(QCompleter * completer);
QSize sizeHint() const override;
QSize minimumSizeHint() const override;
//This function creates the standard context menu which is shown when the
//user clicks on the line edit with the right mouse button.
//It is called from the default contextMenuEvent() handler.
//The popup menu's ownership is transferred to the caller.
QMenu * createStandardContextMenu(); //创建右键快捷菜单。经测试天然就有右键菜单的
using QWidget::addAction; //Adds the action to the list of actions at the position.
void addAction( QAction * action, ActionPosition position);
QAction * addAction(const QIcon & icon, ActionPosition position);
//Creates a new action with the given icon at the position.
//class QMargins { int m_left; int m_top; int m_right; int m_bottom; };
QMargins textMargins() const; //Returns the widget's text margins.
//Sets the margins around the text inside the frame to have the
// sizes left, top, right, and bottom.
void setTextMargins(int left, int top, int right, int bottom);
void setTextMargins(const QMargins & margins);
//Sets the margins around the text inside the frame.
//Returns the cursor position under the point pos.
int cursorPositionAt(const QPoint & pos); //class QPoint { int xp; int yp; };
//Moves the cursor forward steps characters.
//If mark is true each character moved over is added to the selection;
//if mark is false the selection is cleared.
//将光标向前移动几个字符。
//如果标记为真,则每个移动过的字符都会添加到选择中;如果标记为假,则选择会被清除。
void cursorForward (bool mark, int steps = 1); //移动光标一个字符
void cursorBackward(bool mark, int steps = 1);
//将光标向后移动指定数量的字符。
//如果标记为true,则每个移动的字符都会添加到选择中;如果标记为false,则选择将被清除。
//Moves the cursor one word forward . If mark is true, the word is also selected.
void cursorWordForward (bool mark); //移动光标一个单词
void cursorWordBackward(bool mark);
//Moves the cursor one word backward. If mark is true, the word is also selected.
//Returns the index of the first selected character in the line edit ,
// or -1 if no text is selected.
int selectionStart () const;
//Returns the index of the character directly after the selection in the line edit
// or -1 if no text is selected.
int selectionEnd () const;
int selectionLength() const; //Returns the length of the selection.
void setSelection(int start, int length);
//Selects text from position start and for length characters.
// Negative lengths are allowed.
void deselect(); //Deselects any selected text.
//Moves the text cursor to the beginning of the line unless it is already there.
//If mark is true, text is selected towards the first position;
// otherwise, any selected text is unselected if the cursor is moved.
void home(bool mark); //挪光标到首部
void end (bool mark); //挪光标到尾部
//Moves the text cursor to the end of the line unless it is already there.
//If mark is true, text is selected towards the last position;
// otherwise, any selected text is unselected if the cursor is moved.
//如果未选择文本,则删除文本光标左侧的字符并将光标向左移动一个位置。
//如果选择了任何文本,则将光标移动到所选文本的开头,并删除所选文本。
void backspace(); //即向前删除一个字符
void del() ; // 向后删除一个字符
//如果未选择任何文本,则删除文本光标右侧的字符。
//如果选择了任何文本,则将光标移动到所选文本的开头,并删除所选文本。
void insert(const QString & newText); //在行输入框里的光标处插入新文本
//Deletes any selected text, inserts newText, and validates the result.
//If it is valid, it sets it as the new contents of the line edit.
//删除任何选定的文本,插入新文本,并验证结果。如果有效,则将其设置为行编辑的新内容。
public Q_SLOTS:
//void setText(const QString &);
void selectAll(); //选择所有文本(即高亮显示)并将光标移动到末尾。
//这在插入默认值时很有用,因为如果用户在单击小部件之前输入,则所选文本将被删除。
void cut (); //将所选文本复制到剪贴板并删除它,如果有任何内容,并且如果echoMode()为Normal。
//如果当前验证器不允许删除所选文本,则cut()将复制而不删除。
void copy () const; //Copies the selected text to the clipboard,
// if there is any, and if echoMode() is Normal.
void paste(); //将剪贴板中的文本插入到光标位置,删除任何选定的文本,前提是行编辑不是只读的。
//如果最终结果对当前的验证器无效,则不会发生任何事情。
void clear(); //Clears the contents of the line edit.
void undo (); //如果可撤销,则撤销最后一次操作。取消当前选定的任何内容,
//并将选择起始位置更新为当前光标 位置。
void redo (); //Redoes the last operation if redo is available.
Q_SIGNALS:
//每当文本更改时,都会发出此信号。text参数是新的文本。
//与 textEdited ()不同,当文本通过编程更改时,例如通过调用setText()时,也会发出此信号。
//void textChanged(const QString & text);
void textEdited (const QString & text);
//每当编辑文本时都会发出此信号。text参数是新的文本。
//与 textChanged()不同,当通过编程更改文本(例如,通过调用setText())时,不会发出此信号。
//This signal is emitted whenever the cursor moves.
//The previous position is given by oldPos, and the new position by newPos.
void cursorPositionChanged(int oldPos, int newPos); //当输入框内光标移动时触发本信号
void selectionChanged(); //只要被选中的文本有变化,就会触发本信号
//This signal is emitted whenever the selection changes.
//当按下“返回”或“Enter”键时,会发出此信号。
//请注意,如果在行编辑上设置了validator()或 inputMask(),
// 则只有当输入遵循 inputMask()且 validator()返回 QValidator::Acceptable时,
// 才会发出 returnPressed()信号。
void returnPressed (); // enter键会触发本信号。
void editingFinished(); //除了键盘触发,失去焦点也会触发本函数;
//当按下“返回”或“Enter”键时,或者当行编辑失去焦点且其内容自上次发出此信号以来已更改时,
//会发出此信号。请注意,如果在行编辑上设置了validator()或inputMask(),
//并且按下Enter/Return键,则只有在输入遵循 inputMask()且 validator()返回
//QValidator::Acceptable时,才会发出编辑完成()信号。
void inputRejected();
//当用户按下不被认为是可接受输入的键时,会发出此信号。
//例如,如果按下键导致验证器的validate()调用返回Invalid。
//另一个情况是尝试输入超出行编辑最大长度的更多字符。
//如果部分文本被接受,但并非全部,则仍然会发出此信号。
// 例如,如果设置了最大长度,且剪贴板文本在粘贴时超过最大长度。
public:
bool event(QEvent * ) override;
void timerEvent(QTimerEvent * ) override;
protected:
void mousePressEvent(QMouseEvent * ) override;
void mouseMoveEvent(QMouseEvent * ) override;
void mouseReleaseEvent(QMouseEvent * ) override;
void mouseDoubleClickEvent(QMouseEvent * ) override;
void keyPressEvent(QKeyEvent * ) override;
void keyReleaseEvent(QKeyEvent * ) override;
void focusInEvent(QFocusEvent * ) override;
void focusOutEvent(QFocusEvent * ) override;
void paintEvent(QPaintEvent * ) override;
void dragEnterEvent(QDragEnterEvent * ) override;
void dragMoveEvent(QDragMoveEvent * e) override;
void dragLeaveEvent(QDragLeaveEvent * e) override;
void dropEvent(QDropEvent * ) override;
void changeEvent(QEvent * ) override;
void contextMenuEvent(QContextMenuEvent * ) override;
void inputMethodEvent(QInputMethodEvent * ) override;
public:
//Reimplements: QWidget::inputMethodQuery(Qt::InputMethodQuery query) const.
//enum Qt::InputMethodQuery {...}; 似乎是辅助查询输入框的状态的
QVariant inputMethodQuery(Qt::InputMethodQuery ) const override; //继承于父类的成函
Q_INVOKABLE
QVariant inputMethodQuery(Qt::InputMethodQuery property, QVariant argument) const;
protected:
QRect cursorRect() const; //Returns a rectangle that includes the lineedit cursor.
virtual void initStyleOption(QStyleOptionFrame * option) const;
}; //完结 class QLineEdit : public QWidget
QT_END_NAMESPACE
#endif // QLINEEDIT_H
(11)
谢谢