QstackedLayout类可以对一组子窗口部件进行摆放,或者对它分页
1. 先建立这2个页面
n1=new QPushButton(tr("1button"));
n2=new QPushButton(tr("2button"));
QLabel *label1=new QLabel(tr("button"));
QLabel *label2=new QLabel(tr("dff"));
pag1=new QWidget; //第一个页面
QHBoxLayout *layout1=new QHBoxLayout;
layout1->addWidget(label1);
layout1->addWidget(n1);
layout1->addStretch();
pag1->setLayout(layout1);
pag2=new QWidget; //第二个页面
QVBoxLayout *layout2 = new QVBoxLayout;
layout2->addWidget(label2);
layout2->addWidget(n2);
layout2->addStretch();
pag2->setLayout(layout2);
2. 创建左边的选择列表
QListWidget *listWidget=new QListWidget;
listWidget->addItem(tr("app1"));
listWidget->addItem(tr("app2"));
3. 用QstackedLayout将这2个页面组合起来
QStackedLayout *stackLayout=new QStackedLayout;
stackLayout->addWidget(pag1);
stackLayout->addWidget(pag2);
4. 通过信号和槽将两边的部件进行联系
connect(listWidget,SIGNAL(currentRowChanged(int)),stackLayout,SLOT(setCurrentIndex(int)));
5. 最后把各部件用布局管理器进行摆放
QGridLayout *mainLayout = new QGridLayout;
mainLayout->setColumnStretch(0, 1);
mainLayout->setColumnStretch(1, 3);
mainLayout->addWidget(listWidget, 0, 0);
mainLayout->addLayout(stackLayout, 0, 1); /./如果没有这两行,则会出现2个窗口
setLayout(mainLayout);
listWidget->setCurrentRow(0); //显示第一个页面
2010.07.30