줄 단위 편집 예제
'라인 편집' 예제는 QLineEdit 의 다양한 사용 방법을 보여주고, 사용자가 입력한 내용과 출력 결과에 다양한 속성과 유효성 검사기가 미치는 영향을 보여줍니다.

이 예제는 단일 Window 클래스로 구성되어 있으며, 콤보박스에서 항목을 선택하여 변경할 수 있는 다양한 입력 제약 조건과 표시 속성을 가진 라인 편집기들이 포함되어 있습니다. 이러한 요소들을 함께 제시함으로써 개발자가 라인 편집기에 사용할 적절한 속성을 선택하는 데 도움을 주고, 각 유효성 검사기가 사용자 입력에 미치는 영향을 쉽게 비교할 수 있게 합니다.
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 생성자는 라인 에디트, 유효성 검사기 및 콤보박스를 설정하고, 콤보박스의 신호를 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`, ` 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();
}이 슬롯은 라인 편집기가 사용할 새로운 유효성 검사기를 생성하거나, 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;
}
}여기서는 콤보박스의 “ False ” 및 “ True ” 항목을 각각 false 및 true 값과 연결하여, 이 값들을 QLineEdit::setReadOnly() 함수에 전달하도록 했습니다. 이를 통해 사용자는 라인 에디트의 입력 기능을 활성화하거나 비활성화할 수 있습니다.
© 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.