当前位置:网站首页>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 能够在指定的时间间隔触发消息
边栏推荐
- [how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
- Leetcode-1175.Prime Arrangements
- SQL Injection (POST/Select)
- Richview trvstyle liststyle list style (bullet number)
- 双向链表(我们只需要关注插入和删除函数)
- Setting up remote links to MySQL on Linux
- Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
- [556. Next larger element III]
- The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
- The principle of human voice transformer
猜你喜欢

Unity embeddedbrowser browser plug-in event communication

Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.

User and group command exercises

Bidirectional linked list (we only need to pay attention to insert and delete functions)

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

RichView TRVStyle ListStyle 列表样式(项目符号编号)

AI 考高数得分 81,网友:AI 模型也免不了“内卷”!

Golang - command line tool Cobra

TensorBoard可视化处理案例简析

AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
随机推荐
Golang - command line tool Cobra
物联网毕设 --(STM32f407连接云平台检测数据)
Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
MySQL installation, uninstallation, initial password setting and general commands of Linux
Heap structure and heap sort heapify
pytorch 载入历史模型时更换gpu卡号,map_location设置
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
Use and design of Muduo buffer class
Unity EmbeddedBrowser浏览器插件事件通讯
Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
挡不住了,国产芯片再度突进,部分环节已进到4nm
Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
[technology development-24]: characteristics of existing IOT communication technology
常见的几种最优化方法Matlab原理和深度分析
SQL Injection (GET/Select)
Static linked list (subscript of array instead of pointer)
[技术发展-24]:现有物联网通信技术特点
Kivy教程之 如何自动载入kv文件
Swiftui development experience: the five most powerful principles that a programmer needs to master