布局管理器5 -> 栈式布局管理器(QStackedLayout)

布局管理器5 -> 栈式布局管理器(QStackedLayout)

应用范围:如左右滑,上下滑等


  1. 所有组件在垂直于屏幕的方向上被管理
  2. 每次只有一个组件会显示在屏幕上
  3. 只有最顶层的组件会被最终显示
    4.栈式布局管理器不能直接嵌套其它布局管理器(如QBoxLayout或QGridLayout),可以通过中间部件,设置布局管理器

QStackedLayout的用法概要

int addWidget(QWidget* widget);
QWidget* currentWidget();
void setCurrentIndex(int index);
int currentIndex();


----------
void Widget::initControl()
{
    QStackedLayout* sLayout = new QStackedLayout();
    QHBoxLayout* hLayout = new QHBoxLayout();
    QWidget* widget = new QWidget();
    QTimer* timer = new QTimer(this);

    TestBtn1.setText("1st Button");
    TestBtn2.setText("2rd Button");
    TestBtn3.setText("3th Button");
    TestBtn4.setText("Test Button 4: D.T.Software");

    TestBtn2.setParent(widget);
    TestBtn3.setParent(widget);

    hLayout->addWidget(&TestBtn2);
    hLayout->addWidget(&TestBtn3);

    widget->setLayout(hLayout);

    sLayout->addWidget(&TestBtn1); // 0
    sLayout->addWidget(widget); // 1  栈式布局管理器不能直接嵌套其它布局管理器(如QBoxLayout或QGridLayout),可以通过中间部件,设置布局管理器
    sLayout->addWidget(&TestBtn4); // 2

    sLayout->setCurrentIndex(0);

    setLayout(sLayout);

    connect(timer, SIGNAL(timeout()), this, SLOT(timerTimeout()));
    
    timer->start(2000);
}

void Widget::timerTimeout()
{
    QStackedLayout* sLayout = dynamic_cast<QStackedLayout*>(layout()); // 获取栈布局管理器

    if( sLayout != NULL )
    {
        int index = (sLayout->currentIndex() + 1) % sLayout->count();

        sLayout->setCurrentIndex(index);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值