Qt4:Widgetを配置する。


Qt4:百の定石


Widget を奇麗に配置するには,縦に並べる QVBoxLayout?, 横に並べる QHBoxLayout? を組み合わせて使うと便利です。 手順は以下の通りです。

  • 親玉の Layout をそのクラス(のWidget)に登録する。子供の Layout を addLayout() を使って登録する。
  • 子供の Layout 作り、親玉に addLayout() を使って登録する。
  • 子供または,親玉の Layout に ボタンやラベル等の Widget に addWidget() を使って登録する。
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>

最新の20件

2020-12-25 2007-03-14 2006-10-30 2006-11-09 2007-03-14 2006-11-09 2006-11-10 2006-11-09 2008-05-20 2007-11-07 2007-11-01

今日の20件

  • counter: 229
  • today: 1
  • yesterday: 0
  • online: 1