「行単位の編集」の例
「行編集」の例では、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 のもう1つの便利な機能は、その内容を読み取り専用にできることです。このプロパティは、次のウィジェット群において、ラインエディットへのアクセスを制御するために使用されます:
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 」という項目を、QLineEdit::setReadOnly() に渡す値であるfalse およびtrue と単純に紐づけています。これにより、ユーザーはラインエディットへの入力を有効または無効にすることができます。
© 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.