第二十五课、布局管理器(四)------------------狄泰软件学院

一、栈式布局管理器

1、栈式布局管理器(QStatckedLayout)概要

(1)、所有组件垂直于屏幕的方向上被管理

(2)、每次只有一个组件会显示在屏幕上

(3)、只有最顶层的组件会被最终显示

2、栈式布局管理器的特点

(1)、组件大小一致且充满父组件的显示区

(2)、不能直接嵌套其它布局管理器(可以依赖中间组件间接嵌套)

(3)、能够自由切换需要显示的组件

(4)、每次能且仅能显示一个组件

3、QStatckedLayout的用法概要

二、计时器

1、计时器的概念

(1)、计时器是工程开发中非常重要的概念

(2)、计时器用于每隔一定的时间触发一个消息

(3)、计时器消息最终会被转化为函数调用

(4)、宏观上:计时器在每隔时间间隔会调用指定的函数

2、计时器(QTimer)的使用方法

(1)、编写计时器消息处理函数

(2)、在程序中创建计时器对象

(3)、连接计时器消息和消息处理函数

(4)、设置计时器时间间隔并启动计时

 Widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui/QWidget>
#include <QPushButton>

class Widget : public QWidget
{
    Q_OBJECT
private:
    QPushButton Btn1;
    QPushButton Btn2;
    QPushButton Btn3;
    QPushButton Btn4;
    void initControl();
private slots:
    void outtime();
public:
    Widget(QWidget *parent = 0);
    ~Widget();
};

#endif // WIDGET_H

Widget.h

 Widget.cpp

#include "Widget.h"
#include <QStackedLayout>
#include <QHBoxLayout>
#include <QTimer>

Widget::Widget(QWidget *parent)
    : QWidget(parent), Btn1(this), Btn2(this), Btn3(this), Btn4(this)
{
    initControl();
}

void Widget::initControl()
{
    Btn1.setText("Btn1");
    Btn2.setText("Btn2");
    Btn3.setText("Btn3");
    Btn4.setText("Btn4");

    QStackedLayout* slayout = new QStackedLayout();



    QHBoxLayout* hlayout = new QHBoxLayout();//通过间接的方法来嵌套布局管理器
    QWidget* widget = new QWidget();
    Btn2.setParent(widget);
    Btn3.setParent(widget);
    hlayout->addWidget(&Btn2);
    hlayout->addWidget(&Btn3);
    widget->setLayout(hlayout);



    slayout->addWidget(&Btn1);//0
    slayout->addWidget(widget);//1
    slayout->addWidget(&Btn4);//2

    slayout->setCurrentIndex(0);//设置初始要显示的按钮

    setLayout(slayout);

    //计时器的使用
    QTimer* timer = new QTimer();//1.定义对象
    connect(timer, SIGNAL(timeout()), this, SLOT(outtime()));//3.连接信号与槽
    timer->start(2000);//4.启动定时器

}

void Widget::outtime()//2.编写槽函数
{
    QStackedLayout* sLayout = dynamic_cast<QStackedLayout*>(layout());

    if(sLayout != NULL)
    {
        int index = (sLayout->currentIndex() + 1) % sLayout->count();//防止溢出,故取余
        sLayout->setCurrentIndex(index);
    }

}

Widget::~Widget()
{
    
}

Widget.cpp

 main.cpp

#include <QtGui/QApplication>
#include "Widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    
    return a.exec();
}

main.cpp

三、小结

(1)、QStatckedLayout是栈的方式管理界面组件

(2)、QStatckedLayout中的组件最多显示一个

(3)、QStatckedLayout可以自由切换需要显示的组件

(4)、QTimer是Qt中的计时器组件

(5)、QTimer能够在指定的时间间隔触发消息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值