当前位置:网站首页>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 能够在指定的时间间隔触发消息
边栏推荐
- 顺序表(C语言实现)
- Mycms we media mall v3.4.1 release, user manual update
- Golang — template
- Can newly graduated European college students get an offer from a major Internet company in the United States?
- Setting up remote links to MySQL on Linux
- Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
- Record 405 questions about bank callback post request
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- When updating mysql, the condition is a query
- 使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
猜你喜欢

SQL Injection (GET/Search)

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
![[sort] bucket sort](/img/52/95514b5a70cea75821883e016d8adf.jpg)
[sort] bucket sort

Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock

全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场

SQL Injection (AJAX/JSON/jQuery)

Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases

MySQL 数据处理值增删改

Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market

CVPR 2022 | 美团技术团队精选6篇优秀论文解读
随机推荐
Go language unit test 3: go language uses gocovey library to do unit test
Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.
php 迷宫游戏
Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
Screenshot of the operation steps of upload labs level 4-level 9
Go language unit test 4: go language uses gomonkey to test functions or methods
【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明
Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
When updating mysql, the condition is a query
Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
[技術發展-24]:現有物聯網通信技術特點
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
Open PHP error prompt under Ubuntu 14.04
【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
【556. 下一个更大元素 III】
Golang — template
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team