码迷,mamicode.com
首页 > 其他好文 > 详细

第二十五课、布局管理器(四)

时间:2017-02-16 11:06:29      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:stack   不能   span   gui   btn   重要   tla   start   button   

一、栈式布局管理器

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

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

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

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

技术分享

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

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

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

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

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

3、QStatckedLayout的用法概要

技术分享

二、计时器

1、计时器的概念

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

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

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

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

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

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

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

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

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

技术分享
#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
技术分享
#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
技术分享
#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能够在指定的时间间隔触发消息

第二十五课、布局管理器(四)

标签:stack   不能   span   gui   btn   重要   tla   start   button   

原文地址:http://www.cnblogs.com/gui-lin/p/6404261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!