ウィジェット・チュートリアル - レイアウトの使用
通常、子ウィジェットは位置やサイズを明示的に指定するのではなく、レイアウト・オブジェクトを使ってウィンドウ内に配置します。ここでは、ラベル・ウィジェットとライン編集ウィジェットを並べて配置します。
#include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; QLabel *label = new QLabel(QApplication::translate("windowlayout", "Name:")); QLineEdit *lineEdit = new QLineEdit(); QHBoxLayout *layout = new QHBoxLayout(); layout->addWidget(label); layout->addWidget(lineEdit); window.setLayout(layout); window.setWindowTitle( QApplication::translate("windowlayout", "Window layout")); window.show(); return app.exec(); } |
![]() |
作成したlayout
オブジェクトは、addWidget() 関数で供給されたウィジェットの位置とサイズを管理します。レイアウト自体は、setLayout ()の呼び出しでウィンドウ自体に供給されます。レイアウトは、管理を担当するウィジェット(および他のレイアウト)に与える影響によってのみ可視化されます。
上の例では、各ウィジェットの所有権はすぐにはわかりません。親オブジェクトなしでウィジェットとレイアウトを構築しているので、空のウィンドウと、ラベルとライン編集を含む2つの別々のウィンドウが表示されると予想されます。しかし、レイアウトにラベルとライン・エディットを管理するように指示し、ウィンドウにレイアウトを設定すると、ウィジェットとレイアウト自体がウィンドウの子になるように''リペアレント''されます。
© 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.