このページでは

ウィジェットチュートリアル - ネストされたレイアウト

ウィジェットが他のウィジェットを包含できるのと同様に、レイアウトを使用することで、ウィジェットに対してさまざまなレベルのグループ化を行うことができます。ここでは、クエリの結果を表示するテーブルビューの上部、ウィンドウの最上部に、ラベルをラインエディットの横に配置したいとします。

これを実現するために、2つのレイアウトを作成します。queryLayout は、QLabel とQLineEdit ウィジェットを並べて配置したQHBoxLayout です。mainLayout は、queryLayout とQTableView を縦に配置したQVBoxLayout です。

#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWidget window;

    QLabel *queryLabel = new QLabel(
        QApplication::translate("nestedlayouts", "Query:"));
    QLineEdit *queryEdit = new QLineEdit();
    QTableView *resultView = new QTableView();

    QHBoxLayout *queryLayout = new QHBoxLayout();
    queryLayout->addWidget(queryLabel);
    queryLayout->addWidget(queryEdit);

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->addLayout(queryLayout);
    mainLayout->addWidget(resultView);
    window.setLayout(mainLayout);

    // Set up the model and configure the view...
    window.setWindowTitle(
        QApplication::translate("nestedlayouts", "Nested layouts"));
    window.show();
    return app.exec();
}
ネストされたレイアウトを使用してモデルをクエリし、表示するためのウィジェット

なお、mainLayout のaddLayout()関数を呼び出すことで、queryLayout をresultView テーブルの上部に挿入しています。

QTableView ウィジェットに表示されるデータを含むモデルを設定するコード(resultView )は省略しました。完全を期すため、以下にこれを示します。

QHBoxLayout やQVBoxLayout に加え、Qtではより複雑なユーザーインターフェースを構築するためのQGridLayout およびQFormLayout クラスも提供しています。これらは、Qt Widgets Designer を実行すると確認できます。

モデルの設定

上記のコードでは、レイアウトの使用に焦点を当てるため、テーブルのデータの出所については示しませんでした。ここでは、モデルが行に対応する複数の項目を保持しており、各項目は2つの列分のデータを含むように設定されていることがわかります。

    QStandardItemModel model;
    model.setHorizontalHeaderLabels({ QApplication::translate("nestedlayouts", "Name"),
                                      QApplication::translate("nestedlayouts", "Office") });

    const QStringList rows[] = {
        QStringList{ QStringLiteral("Verne Nilsen"), QStringLiteral("123") },
        QStringList{ QStringLiteral("Carlos Tang"), QStringLiteral("77") },
        QStringList{ QStringLiteral("Bronwyn Hawcroft"), QStringLiteral("119") },
        QStringList{ QStringLiteral("Alessandro Hanssen"), QStringLiteral("32") },
        QStringList{ QStringLiteral("Andrew John Bakken"), QStringLiteral("54") },
        QStringList{ QStringLiteral("Vanessa Weatherley"), QStringLiteral("85") },
        QStringList{ QStringLiteral("Rebecca Dickens"), QStringLiteral("17") },
        QStringList{ QStringLiteral("David Bradley"), QStringLiteral("42") },
        QStringList{ QStringLiteral("Knut Walters"), QStringLiteral("25") },
        QStringList{ QStringLiteral("Andrea Jones"), QStringLiteral("34") }
    };

    QList<QStandardItem *> items;
    for (const QStringList &row : rows) {
        items.clear();
        for (const QString &text : row)
            items.append(new QStandardItem(text));
        model.appendRow(items);
    }

    resultView->setModel(&model);
    resultView->verticalHeader()->hide();
    resultView->horizontalHeader()->setStretchLastSection(true);

モデルとビューの使用方法については、「Item Views の例」および「モデル/ビュープログラミングの概要」で解説されています。

サンプルプロジェクト @ 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.