Qt学习25 布局管理器(4)
最特殊的布局管理器
- 栈布局管理器( )
- 所有组件在 管理方向
- 每次 会显示在屏幕上
- 只有 最终会显示
- 栈式布局管理器的特点
- 显示区充满件的显示区
- 其它布局管理器
- 能够 要显示的组件
- 每次只能
- 的用法概要
int addWidget(QWidget* widget)
QWidget* currentWidget()
void setCurrentIndex(int index)
int currentIndex()
编程实验 - 栈式布局初探
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QPushButton> class Widget : public QWidget {
Q_OBJECT private: QPushButton TestBtn1; QPushButton TestBtn2; QPushButton TestBtn3; QPushButton TestBtn4; void initBtn(); void initControl(); public: Widget(QWidget *parent = 0); ~Widget(); }; #endif // WIDGET_H
#include "Widget.h" #include <QStackedLayout> #include <QHBoxLayout> Widget::Widget(QWidget *parent) QWidget(parent), TestBtn1(this), TestBtn4(this) {
initBtn(); initControl(); } Widget::~Widget() {
} void Widget::initBtn() {
TestBtn1.setText("1st Button"); TestBtn2.setText("2rd Button"); TestBtn3.setText("3th Button"); TestBtn4.setText("Test Button 4: D.T.Software"); } void Widget::initControl() {
// 栈式布局管理器 QStackedLayout* sLayout = new QStackedLayout(); sLayout->addWidget(&TestBtn1); // 因为栈式布局管理器不能直接嵌套其他布局管理器,所以用QWidget做中间层 QWidget* w = new QWidget(); // 在QWidget里设置水平布局管理器 QHBoxLayout* hLayout = new QHBoxLayout(); hLayout->addWidget(&TestBtn2); hLayout->addWidget(&TestBtn3); w->setLayout(hLayout); // 再把QWidget添加进栈式布局管理器中,起到间接嵌套水平布局管理器的效果 sLayout->addWidget(w); sLayout->addWidget(&TestBtn4); sLayout->setCurrentIndex(2); //0:Btn1, 1:Btn2、Btn3, 2:Btn4 setLayout(sLayout); }
#include "Widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
计时器的概念
- 计时器是 非常重要的角色
- 计时器用于
- 计时器 最终会被转化为
- 宏观上
- 计时器在每个事件间隔会调用指定的函数
Qt中的计时器
-
计时器 的使用方法
1.编写计时器消息处理函数
2.在程序中创建计时器对象
3.连接计时器消息和消息处理函数
4.设置计时器时间间隔并启动计时
编程实验 - 计时器的使用
新增内容:
#ifndef WIDGET_H
#define WIDGET_H
class Widget : public QWidget
{
private slots:
void timerTimeout();
};
#endif // WIDGET_H
#include <QTimer>
void Widget::initControl()
{
// 上一个实验中的内容
// 定时器添加内容
QTimer* timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerTimeout()));
timer->start(2000);
}
void Widget::timerTimeout()
{
// layout()获取当前窗口的布局管理器
QStackedLayout* sLayout = dynamic_cast<QStackedLayout*>(layout());
if (sLayout != NULL) {
int index = (sLayout->currentIndex()+1) % sLayout->count();
sLayout->setCurrentIndex(index);
}
}
小结
- 以栈的方式管理界面组件
- 中的组件
- 可以 需要显示的组件
- 是Qt中的计时器组件
- 能够在指定的时间间隔触发消息