줄 편집 예제

줄 편집 예제는 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 클래스의 슬롯에 연결하고, 레이아웃에서 자식 위젯을 정렬하는 데 사용됩니다.

QLineEdit::echoMode 속성을 보여줄 수 있도록 레이블, 콤보박스, 줄 편집을 담을 group box 을 만드는 것부터 시작합니다:

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, echoLineEditechoGroup 그룹 상자 안에 세로 레이아웃으로 배치됩니다.

마찬가지로 그룹 상자와 위젯 모음을 구성하여 QIntValidatorQDoubleValidator 이 한 줄 편집 콘텐츠에 미치는 영향을 표시합니다:

    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");

QLineEditinput 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();
}

이 슬롯은 줄 편집에 사용할 새 유효성 검사기를 만들거나 0 포인터로 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;
    }
}

여기서는 콤보박스의 FalseTrue 항목을 falsetrue 값과 연결하여 QLineEdit::setReadOnly()로 전달하기만 하면 됩니다. 이렇게 하면 사용자가 줄 편집에 대한 입력을 활성화 및 비활성화할 수 있습니다.

예제 프로젝트 @ code.qt.io

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