当前位置:网站首页>Qt学习25 布局管理器(四)
Qt学习25 布局管理器(四)
2022-07-03 13:23:00 【一个小黑酱】
Qt学习25 布局管理器(四)
最特别的布局管理器
- 栈式布局管理器( QStackedLayout )
- 所有组件在 垂直于屏幕 的方向上被管理
- 每次 只有一个组件 会显示在屏幕上
- 只有 最顶层的组件 会被最终显示
- 栈式布局管理器的特点
- 组件大小一致 且充满父组件的显示区
- 不能直接嵌套 其他布局管理器
- 能够 自由切换 需要显示的组件
- 每次能且仅能 显示一个组件
- QStackedLayout 的用法概要
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中的计时器
计时器 ( QTimer ) 的使用方法
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);
}
}
小结
- QStackedLayout 以栈的方式管理界面组件
- QStackedLayout 中的组件 最多只有一个显示
- QStackedLayout 可以 自由切换 需要显示的组件
- QTimer 是Qt中的计时器组件
- QTimer 能够在指定的时间间隔触发消息
边栏推荐
- 网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
- Depth and breadth first traversal of tree (regardless of binary tree)
- The shadow of the object at the edge of the untiy world flickers, and the shadow of the object near the far point is normal
- Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
- MySQL 数据增删改查综合案例
- Spark practice 1: build spark operation environment in single node local mode
- [quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
- Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
- untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
- 【556. 下一个更大元素 III】
猜你喜欢
SQL Injection (POST/Select)
Internet of things completion -- (stm32f407 connects to cloud platform detection data)
SQL Injection (GET/Select)
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
Universal dividend source code, supports the dividend of any B on the BSC
[technology development-24]: characteristics of existing IOT communication technology
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.
Go language unit test 4: go language uses gomonkey to test functions or methods
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
随机推荐
Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
挡不住了,国产芯片再度突进,部分环节已进到4nm
Open PHP error prompt under Ubuntu 14.04
Spark practice 1: build spark operation environment in single node local mode
Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.
This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
研发团队资源成本优化实践
Libuv Library - Design Overview (Chinese version)
Heap structure and heap sort heapify
服务器硬盘冷迁移后网卡无法启动问题
【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
SQL Injection (AJAX/JSON/jQuery)
The principle of human voice transformer
Golang — template
Go 1.16.4: manage third-party libraries with Mod
Go language web development series 27: Gin framework: using gin swagger to implement interface documents
Software testing is so hard to find, only outsourcing offers, should I go?
Setting up remote links to MySQL on Linux
Depth and breadth first traversal of tree (regardless of binary tree)
SVN添加文件时的错误处理:…\conf\svnserve.conf:12: Option expected