当前位置:网站首页>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 能够在指定的时间间隔触发消息
边栏推荐
- Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
- TensorBoard可视化处理案例简析
- PHP maze game
- Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
- 研发团队资源成本优化实践
- Ubuntu 14.04 下开启PHP错误提示
- Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
- Go language unit test 4: go language uses gomonkey to test functions or methods
- CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
- Multi table query of MySQL - multi table relationship and related exercises
猜你喜欢
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
SQL Injection (POST/Search)
Kivy教程之 如何自动载入kv文件
[email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g"/>
Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
8 Queen question
掌握Cypress命令行选项,是真正掌握Cypress的基础
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
There is nothing new under the sun. Can the meta universe go higher?
AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
随机推荐
Unity Render Streaming通过Js与Unity自定义通讯
JS convert pseudo array to array
SVN添加文件时的错误处理:…\conf\svnserve.conf:12: Option expected
Ubuntu 14.04 下开启PHP错误提示
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
[技術發展-24]:現有物聯網通信技術特點
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
JVM系列——概述,程序计数器day1-1
Golang - command line tool Cobra
Go language unit test 3: go language uses gocovey library to do unit test
GoLand 2021.2 configure go (go1.17.6)
GoLand 2021.1: rename the go project
JSON serialization case summary
SQL Injection (POST/Search)
TensorBoard可视化处理案例简析
The solution of Chinese font garbled code in keil5
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases