//底层widget
QWidget* wd1 = new QWidget();
QHBoxLayout* hbox = new QHBoxLayout(wd1);
QWidget* wd1Left = new QWidget();
wd1Left->setStyleSheet("background-color: rgb(0, 200, 0);");
QWidget* wd1Right = new QWidget();
wd1Right->setStyleSheet("background-color: rgb(0, 0, 10);");
hbox->addWidget(wd1Left);
hbox->addWidget(wd1Right);
wd1->setLayout(hbox);
//顶层widget
QWidget* wd2 = new QWidget();
QHBoxLayout* hbox2 = new QHBoxLayout(wd2);
QWidget* wd2Left = new QWidget();
wd2Left->setStyleSheet("background-color: rgb(10, 50, 20,0);");
QWidget* wd2Center = new QWidget();
wd2Center->setStyleSheet("background-color: rgb(200, 0, 0, 80);");
QWidget* wd2Right = new QWidget();
wd2Right->setStyleSheet("background-color: rgb(60, 128, 10,0);");
hbox2->addWidget(wd2Left);
hbox2->addWidget(wd2Center);
hbox2->addWidget(wd2Right);
wd2->setLayout(hbox2);
//
QStackedLayout * sBoxLayout = new QStackedLayout();
sBoxLayout->setStackingMode(QStackedLayout::StackAll);
sBoxLayout->addWidget(wd1);
sBoxLayout->addWidget(wd2);
wd1->stackUnder(wd2);
//
QVBoxLayout* layoutMain = new QVBoxLayout();
layoutMain->addLayout(sBoxLayout);
this->centralWidget()->setLayout(layoutMain);
Qt 之层叠QWidget,使用QStackedLayout
最新推荐文章于 2024-09-26 14:32:39 发布
本文介绍了一个使用Qt进行界面布局管理的例子。通过QStackedLayout和QHBoxLayout实现两个不同层级的Widget展示,并通过stackUnder方法调整堆叠顺序。该示例展示了如何设置不同颜色背景的区域以区分不同的Widget。
摘要由CSDN通过智能技术生成