本页内容

行编辑示例

“行编辑”示例展示了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");

文本对齐效果通过另一组控件进行演示:

    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 的另一个有用功能是能够将其内容设为只读。在以下控件组中,此属性用于控制对行编辑框的访问权限:

    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()方法。这样,用户就可以启用或禁用行编辑框的输入功能。

示例项目 @ code.qt.io

© 2026 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.