Common properties, methods and signals of QLineEdit control
1. Common properties and methods of QLineEdit control
2. Common signals of QLineEdit control
QLineEdit: Single-line text input box control
1. Commonly used properties and methods of QLineEdit control:
1. text:
Description: Get or set the text content in the text box.
Usage: You can usesetText()
the method to set the text content of the text box, or use the text() method to get the current text in the text box.
setText(const QString &text):
2. placeholderText:
Description: Set a placeholder text, displayed when the text box is empty, to prompt the user to enter the expected content.
Usage: UsesetPlaceholderText()
the method to set placeholder text, usually used to indicate what content the user should input.
setPlaceholderText(const QString &text):
edt->setPlaceholderText("请输入密码")://提示输入文本
3. maxLength:
Description: Set the maximum number of characters allowed to be entered in the text box. Usage: Set the maximum character limit
through the method, which is useful for limiting the length of user input.setMaxLength()
setMaxLength(int length):
4. echoMode:
Description: Set the text display mode, used to handle the input of sensitive information such as passwords.
Usage: UsesetEchoMode()
the method to set the display mode, you can choose to display normal text, password characters or not display.
setEchoMode(EchoMode mode):
edt->setEchoMode(QLineEdit::Password); //设置密码隐藏模式
edt->setEchoMode(QLineEdit::PasswordEchoOnEdit): //设置鼠标点击别的地方密码隐藏模式
5. readOnly:
Description: Set whether the text box is in read-only mode, that is, users cannot edit the content.
Usage: UsesetReadOnly()
the method to set whether it is read-only, suitable for scenarios where information is displayed but editing is not allowed.
setReadOnly(bool readOnly):
6. selectedText(): Get the text data selected by the cursor
edt->selectedText();
2. Common signals of QLineEdit control
1. textChanged(const QString &text):
Description: This signal is triggered when the text content changes.
Parameters: text is the current text content.
Usage: Connect to the slot function of this signal to perform corresponding operations when the text changes.
2. editingFinished():
Description: This signal is triggered when editing is completed (for example, the user presses the Enter key).
Usage: Connect to the slot function of this signal to perform corresponding operations when editing is completed.
3. returnPressed():
Description: This signal is triggered when the user presses the Enter key.
Usage: Connect to the slot function of this signal to perform the corresponding operation when the user presses the Enter key.
4. cursorPositionChanged(int oldPos, int newPos):
Description: This signal is triggered when the cursor position changes.
Parameters: oldPos is the cursor position before change, newPos is the cursor position after change.
Usage: Connect to the slot function of this signal to perform corresponding operations when the cursor position changes.
5. selectionChanged():
Description: This signal is triggered when the selected text changes.
Usage: Connect to the slot function of this signal to perform corresponding operations when the selected text changes.
6. textEdited(const QString &text):
Description: This signal is triggered when the text content is edited (not necessarily changed).
Parameters: text is the current text content.
Usage: Connect to the slot function of this signal to perform corresponding operations when the text is edited.
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setFixedSize(640,480);
lab = new QLabel("行编辑器", this);
// ed1 = new QLineEdit(QLine(), this);
// ed1->setFixedSize(100, 50);
//setEchoMode(QLineEdit::Password):设置密码隐藏模式
ui->etd1->setEchoMode(QLineEdit::Password);
//setEchoMode(QLineEdit::PasswordEchoOnEdit):设置鼠标点击别的地方密码隐藏模式
ui->etd2->setEchoMode(QLineEdit::PasswordEchoOnEdit);
//setPlaceholderText():提示输入文本
ui->etd3->setPlaceholderText("密码");
//绑定事件(信号发送者, 触发事件地址, 信号接收者, 触发事件槽函数)
connect(ui->etd, &QLineEdit::editingFinished, this, &Widget::textFinshed);
//
//connect(ui->etd2, &QLineEdit:: textChanged, this, QOverload<QString>::of(&Widget::te));
}
Widget::~Widget()
{
delete ui;
}
void Widget::textFinshed()
{
ui->label->setText(ui->etd->text());
}
/*
* 设计器右键转到槽:自动声明定义槽函数,该函数已经绑定了信号
* 槽函数语法:void on_objectName_signal();
* on_objectName控件的signal信号被触发,系统自动调用该槽函数
*/
void Widget::on_etd1_selectionChanged() //on_对象名_信号()
{
//selectedText():获得光标选中的文本数据
ui->label_2->setText(ui->etd1->selectedText());
}
/*
* 当信号有形参,目的是触发信号时,传递数据。关联的槽函数,如果需要接收数据,则必须设计 同类型的形爹,如果不需要数据,则不必设置形参
* 信号与槽函数的参数个数关系: 槽函数形参个数 <= 信号的形参个数
*/
void Widget::on_etd2_textChanged()
{
ui->label_3->setText(ui->etd2->text());
}
void Widget::on_etd2_textChanged(const QString text)
{
ui->label_3->setText(text);
}