1、切换前垂直布局
2、切换后水平布局
3、关键代码
qDebug() << "开始切换布局";
QWidget *widget = centralWidget();
QLayout *layout = widget->layout();
if(layout)
{
while(layout->count())
{
QLayoutItem *item = layout->takeAt(0);
if(item->layout())
{
delete item->layout();
}
else
{
if(item->widget())
{
delete item->widget();
}
}
delete item;
}
delete layout;
}
layout = new MyHBoxLayout;
QLabel *label1 = new MyLabel("Label 1");
QLabel *label2 = new MyLabel("Label 2");
QLabel *label3 = new MyLabel("Label 3");
label1->setStyleSheet("QLabel {background : red}");
label2->setStyleSheet("QLabel {background : green}");
label3->setStyleSheet("QLabel {background : blue}");
layout->addWidget(label1);
layout->addWidget(label2);
layout->addWidget(label3);
widget->setLayout(layout);
qDebug() << "完成切换布局";