行編集の例

行編集の例では、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();

この時点では、どのウィジェットもレイアウトに配置されていません。最終的には、echoLabelechoComboBoxechoLineEdit を、echoGroup グループ・ボックス内の縦型レイアウトに配置します。

同様に、グループボックスとウィジェットのコレクションを作成し、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");

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;
    }
}

ここでは、コンボボックスの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.