行编辑示例
行编辑示例演示了QLineEdit 的多种使用方式,并显示了各种属性和验证器对用户提供的输入和输出的影响。
该示例由一个Window
类组成,其中包含一系列具有不同输入限制和显示属性的行编辑选项,这些属性可通过从组合框中选择项目进行更改。将这些属性放在一起显示,有助于开发人员选择合适的属性与行编辑一起使用,并可轻松比较每个验证器对用户输入的影响。
窗口类定义
Window
类继承于QWidget ,包含一个构造函数和几个插槽:
class Window : public QWidget { Q_OBJECT public: Window(QWidget *parent = nullptr); public slots: void echoChanged(int); void validatorChanged(int); void alignmentChanged(int); void inputMaskChanged(int); void accessChanged(int); private: QLineEdit *echoLineEdit; QLineEdit *validatorLineEdit; QLineEdit *alignmentLineEdit; QLineEdit *inputMaskLineEdit; QLineEdit *accessLineEdit; };
当在相关组合框中选择了新的校验器时,这些槽将用于更新特定行编辑所使用的校验器类型。行编辑内容存储在窗口中,供这些槽使用。
窗口类的实现
Window
构造函数用于设置行编辑、验证器和组合框,将组合框的信号连接到Window
类的插槽,并在布局中安排子窗口部件。
首先,我们构建了一个group box ,用于放置标签、组合框和行编辑器,以便演示QLineEdit::echoMode 属性:
Window::Window(QWidget *parent) : QWidget(parent) { QGroupBox *echoGroup = new QGroupBox(tr("Echo")); QLabel *echoLabel = new QLabel(tr("Mode:")); QComboBox *echoComboBox = new QComboBox; echoComboBox->addItem(tr("Normal")); echoComboBox->addItem(tr("Password")); echoComboBox->addItem(tr("PasswordEchoOnEdit")); echoComboBox->addItem(tr("No Echo")); echoLineEdit = new QLineEdit; echoLineEdit->setPlaceholderText("Placeholder Text"); echoLineEdit->setFocus();
此时,这些部件都还没有按布局排列。最终,echoLabel
、echoComboBox
和echoLineEdit
将以垂直布局放置在echoGroup
组框内。
同样,我们构建了组框和部件集合,以显示QIntValidator 和QDoubleValidator 对行编辑内容的影响:
QGroupBox *validatorGroup = new QGroupBox(tr("Validator")); QLabel *validatorLabel = new QLabel(tr("Type:")); QComboBox *validatorComboBox = new QComboBox; validatorComboBox->addItem(tr("No validator")); validatorComboBox->addItem(tr("Integer validator")); validatorComboBox->addItem(tr("Double validator")); validatorLineEdit = new QLineEdit; validatorLineEdit->setPlaceholderText("Placeholder Text");
文本对齐由另一组 widget 演示:
QGroupBox *alignmentGroup = new QGroupBox(tr("Alignment")); QLabel *alignmentLabel = new QLabel(tr("Type:")); QComboBox *alignmentComboBox = new QComboBox; alignmentComboBox->addItem(tr("Left")); alignmentComboBox->addItem(tr("Centered")); alignmentComboBox->addItem(tr("Right")); alignmentLineEdit = new QLineEdit; alignmentLineEdit->setPlaceholderText("Placeholder Text");
QLineEdit input masks这些部件只允许用户在行编辑器中输入符合简单规范的字符。我们构建了一组小工具来演示预定义掩码的选择:
QGroupBox *inputMaskGroup = new QGroupBox(tr("Input mask")); QLabel *inputMaskLabel = new QLabel(tr("Type:")); QComboBox *inputMaskComboBox = new QComboBox; inputMaskComboBox->addItem(tr("No mask")); inputMaskComboBox->addItem(tr("Phone number")); inputMaskComboBox->addItem(tr("ISO date")); inputMaskComboBox->addItem(tr("License key")); inputMaskLineEdit = new QLineEdit; inputMaskLineEdit->setPlaceholderText("Placeholder Text");
QLineEdit 的另一个有用功能是将其内容设置为只读。该属性用于控制对下面一组 widget 中的行编辑的访问:
QGroupBox *accessGroup = new QGroupBox(tr("Access")); QLabel *accessLabel = new QLabel(tr("Read-only:")); QComboBox *accessComboBox = new QComboBox; accessComboBox->addItem(tr("False")); accessComboBox->addItem(tr("True")); accessLineEdit = new QLineEdit; accessLineEdit->setPlaceholderText("Placeholder Text");
现在所有的子窗口部件都已构建完成,我们将组合框的信号连接到Window
对象的插槽:
connect(echoComboBox, &QComboBox::activated, this, &Window::echoChanged); connect(validatorComboBox, &QComboBox::activated, this, &Window::validatorChanged); connect(alignmentComboBox, &QComboBox::activated, this, &Window::alignmentChanged); connect(inputMaskComboBox, &QComboBox::activated, this, &Window::inputMaskChanged); connect(accessComboBox, &QComboBox::activated, this, &Window::accessChanged);
每个连接都使用QComboBox::activated() 信号,为插槽提供一个整数。这将用于有效地更改每个插槽中相应的行编辑。
我们将每个组合框、行编辑器和标签放入每个组框的布局中,从echoGroup
组框的布局开始:
QGridLayout *echoLayout = new QGridLayout; echoLayout->addWidget(echoLabel, 0, 0); echoLayout->addWidget(echoComboBox, 0, 1); echoLayout->addWidget(echoLineEdit, 1, 0, 1, 2); echoGroup->setLayout(echoLayout);
其他布局的构建方法相同:
QGridLayout *validatorLayout = new QGridLayout; validatorLayout->addWidget(validatorLabel, 0, 0); validatorLayout->addWidget(validatorComboBox, 0, 1); validatorLayout->addWidget(validatorLineEdit, 1, 0, 1, 2); validatorGroup->setLayout(validatorLayout); QGridLayout *alignmentLayout = new QGridLayout; alignmentLayout->addWidget(alignmentLabel, 0, 0); alignmentLayout->addWidget(alignmentComboBox, 0, 1); alignmentLayout->addWidget(alignmentLineEdit, 1, 0, 1, 2); alignmentGroup-> setLayout(alignmentLayout); QGridLayout *inputMaskLayout = new QGridLayout; inputMaskLayout->addWidget(inputMaskLabel, 0, 0); inputMaskLayout->addWidget(inputMaskComboBox, 0, 1); inputMaskLayout->addWidget(inputMaskLineEdit, 1, 0, 1, 2); inputMaskGroup->setLayout(inputMaskLayout); QGridLayout *accessLayout = new QGridLayout; accessLayout->addWidget(accessLabel, 0, 0); accessLayout->addWidget(accessComboBox, 0, 1); accessLayout->addWidget(accessLineEdit, 1, 0, 1, 2); accessGroup->setLayout(accessLayout);
最后,我们将每个组框放入Window
对象的网格布局中,并设置窗口标题:
QGridLayout *layout = new QGridLayout; layout->addWidget(echoGroup, 0, 0); layout->addWidget(validatorGroup, 1, 0); layout->addWidget(alignmentGroup, 2, 0); layout->addWidget(inputMaskGroup, 0, 1); layout->addWidget(accessGroup, 1, 1); setLayout(layout); setWindowTitle(tr("Line Edits")); }
当用户更改组合框时,槽会对发出的信号做出响应。
当Echo 组框的组合框发生变化时,echoChanged()
插槽就会被调用:
void Window::echoChanged(int index) { switch (index) { case 0: echoLineEdit->setEchoMode(QLineEdit::Normal); break; case 1: echoLineEdit->setEchoMode(QLineEdit::Password); break; case 2: echoLineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit); break; case 3: echoLineEdit->setEchoMode(QLineEdit::NoEcho); break; } }
该槽会更新同一组框中的行编辑,使其使用与组合框中描述的条目相对应的回声模式。
当Validator 组框的组合框发生变化时,validatorChanged()
插槽将被调用:
void Window::validatorChanged(int index) { switch (index) { case 0: validatorLineEdit->setValidator(nullptr); break; case 1: validatorLineEdit->setValidator(new QIntValidator( validatorLineEdit)); break; case 2: validatorLineEdit->setValidator(new QDoubleValidator(-999.0, 999.0, 2, validatorLineEdit)); break; } validatorLineEdit->clear(); }
该槽要么为行编辑创建一个新的验证器,要么通过调用QLineEdit::setValidator() 删除正在使用的验证器,并将指针置零。在这种情况下,我们会清除行编辑器,以确保新的验证器最初能得到有效的输入。
当Alignment 组框的组合框发生变化时,将调用alignmentChanged()
槽:
void Window::alignmentChanged(int index) { switch (index) { case 0: alignmentLineEdit->setAlignment(Qt::AlignLeft); break; case 1: alignmentLineEdit->setAlignment(Qt::AlignCenter); break; case 2: alignmentLineEdit->setAlignment(Qt::AlignRight); break; } }
这将改变文本在行编辑器中的显示方式,使其与组合框中选择的描述相对应。
inputMaskChanged()
插槽处理Input Mask 组框中组合框的更改:
void Window::inputMaskChanged(int index) { switch (index) { case 0: inputMaskLineEdit->setInputMask(""); break; case 1: inputMaskLineEdit->setInputMask("+99 99 99 99 99;_"); break; case 2: inputMaskLineEdit->setInputMask("0000-00-00"); inputMaskLineEdit->setText("00000000"); inputMaskLineEdit->setCursorPosition(0); break; case 3: inputMaskLineEdit->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#"); break; } }
相关组合框中的每个条目都与输入掩码相关联。我们使用合适的字符串调用QLineEdit::setInputMask() 函数来设置新的屏蔽;如果使用空字符串,屏蔽将被禁用。
accessChanged()
槽用于处理Access 组框中组合框的更改:
void Window::accessChanged(int index) { switch (index) { case 0: accessLineEdit->setReadOnly(false); break; case 1: accessLineEdit->setReadOnly(true); break; } }
在这里,我们只需将组合框中的False 和True 项与false
和true
的值关联起来,并将其传递给QLineEdit::setReadOnly() 即可。这样,用户就可以启用或禁用行编辑输入。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.