Widget を奇麗に配置するには,縦に並べる QVBoxLayout?, 横に並べる QHBoxLayout? を組み合わせて使うと便利です。 手順は以下の通りです。
ex. QVBoxLayout* vMainLayout = new QVBoxLayout(this) ; // this に注目 QHBoxLayout* hLayout1 = new QHBoxLayout ; // this がない。 vMainLayout->addLayout( hLayout1 ) ; QLabel* l1 = new QLabel(tr("hoge")) ; // this がない QSpinBox* s1 = new QSpinBox ; s1->setRange(0, 100) ; hLayout1->addWidget(l1) ; hLayout1->addWidget(s1) ; : QHBoxLayout* bottomLayout = new QHBoxLayout ; QPushButton* closeButton = new QPushButton(tr("O.K.")) ; QPushButton* cancelButton = new QPushButton(tr("Cancel")) ; connect(closeButton, SIGNAL(clicked()), this, SLOT(ok())) ; connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())) ; bottomLayout->addStretch(50) ; bottomLayout->addWidget(closeButton) ; bottomLayout->addWidget(cancelButton) ; vMainLayout->addLayout(bottomLayout) ;
#include <QBoxLayout> #include <QLabel> #include <QPushButton> #include <QSpinBox> #include <QCheckBox>