Qt中最为重要的三种布局管理器是:QHBoxLayout,QVBoxLayout,QGridLayout.这三个类从QLayout派生出来。
QHBoxLayout: 水平布局类
QVBoxLayout: 使用垂直布局类
QGridLayout : 使用网格布局类
具体用法如下:
Layout::Layout(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QWidget *w=new QWidget;
QLabel *label1=new QLabel;
QLabel *label2=new QLabel;
QLineEdit *lineEdit1=new QLineEdit;
label1->setText(tr("One"));
label2->setText(tr("Two"));
QLineEdit *lineEdit2=new QLineEdit;
QCheckBox *checkBox=new QCheckBox;
QTreeWidget *treeWidget=new QTreeWidget;
QLabel *label3=new QLabel;
label3->setText(tr("Three"));
QPushButton *pushBtn1=new QPushButton;
QPushButton *pushBtn2=new QPushButton;
QPushButton *pushBtn3=new QPushButton;
QPushButton *pushBtn4=new QPushButton;
QGridLayout *leftlayout=new QGridLayout;
leftlayout->addWidget(label1,0,0);
leftlayout->addWidget(lineEdit1,0,1);
leftlayout->addWidget(label2,1,0);
leftlayout->addWidget(lineEdit2,1,1);
leftlayout->addWidget(checkBox,2,0,1,2);
leftlayout->addWidget(treeWidget,3,0,1,2);
leftlayout->addWidget(label3,4,0,1,2);
QVBoxLayout *rightlayout=new QVBoxLayout;
rightlayout->addWidget(pushBtn1);
rightlayout->addWidget(pushBtn2);
rightlayout->addWidget(pushBtn3);
rightlayout->addStretch();
rightlayout->addWidget(pushBtn4);
QHBoxLayout *mainlayout=new QHBoxLayout(w);
mainlayout->addLayout(leftlayout);
mainlayout->addLayout(rightlayout);
w->setLayout(mainlayout);
w->show();
setWindowTitle(tr("Find"));
}