当前位置:网站首页>QT realizes interface sliding switching effect
QT realizes interface sliding switching effect
2022-07-04 19:25:00 【Sanlei Technology】
Catalog
One 、Qt Realize interface sliding switching effect
3、 ... and 、 Main function explanation
4、2 Slide effect of previous page
5、 ... and 、 Source code address
One 、Qt Realize interface sliding switching effect
The effect is as follows , Sliding effect moves up and down the screen .

Two 、 Design thinking
utilize QStackWidget Store pages , Because there are fewer pages , So I store all the pages directly in QStachWidget in , If there are relatively many pages , You can use the method of rendering .
And then use show Function shows the contents of two pages at the same time , This is very important , If you use setCurrentIndex Only one interface will be displayed , In this way, there will not be two interfaces at the same time .
Use QPropertyAnimation as well as QParallelAnimationGroup To animate interface switching .
When the page moves left , Remove the original screen to the left , Move the current interface from the right to the current interface position .
When the page moves to the right , Remove the original screen to the right , Move the current interface from the left to the display position
3、 ... and 、 Main function explanation
QPropertyAnimation: Animation class , If you only control the animation of one interface, you can directly set the animation effect ,start Function to start the animation effect .
QParallelAnimationGroup: Animation group class , Control a set of animations to run at the same time , We control two interfaces here, so we need to use QParallelAnimationGroup To control the animation of the two interfaces .
QStackedWidget: Used to store multiple interfaces , When the interface needs to be displayed, you can use setCurrentIndex Show the current page .
Four 、 Source code parsing
4、1 Initialization interface
stay QStatchWidget Add multiple interfaces in . Because this is the initialization of the game interface , Every page 25 topic , Altogether 515 questions , The total number of pages turned int(515/25).
#define MAX_NUM 515
LevelMainWidget::LevelMainWidget(QWidget* parent)
: QWidget(parent)
, m_ptrStackWidget(new QStackedWidget(this))
, m_ptrLayoutMain(new QHBoxLayout)
, m_ptrBtnPre(new QPushButton(" the previous ", this))
, m_ptrBtnNext(new QPushButton(" next ", this))
, m_bDonghua(false)
{
// Add interface
for (int i = 0; i < 515; i += 25) {
int start = i + 1;
int end = i + 25;
if (end > 515) {
end = 515;
}
LevelWidget* lvlWidget = new LevelWidget(start, end);
m_listLevelWidget.append(lvlWidget);
m_ptrStackWidget->addWidget(lvlWidget);
connect(lvlWidget, &LevelWidget::sigBtnClick, this,
&LevelMainWidget::slotBtnLevel);
}
// Set the index of the currently displayed interface .
m_ptrStackWidget->setCurrentIndex(0);
// Add previous button
m_ptrLayoutMain->addWidget(m_ptrBtnPre);
// Add the displayed interface
m_ptrLayoutMain->addWidget(m_ptrStackWidget);
// Add next page button
m_ptrLayoutMain->addWidget(m_ptrBtnNext);
setFixedSize(500, 500);
setLayout(m_ptrLayoutMain);
initConnect();
}
void LevelMainWidget::initConnect()
{
connect(m_ptrBtnPre, SIGNAL(clicked()), this, SLOT(slotBtnPre()));
connect(m_ptrBtnNext, SIGNAL(clicked()), this, SLOT(slotBtnNext()));
}
4、2 Slide effect of previous page
Get the width and height of the display interface , You need to use the mobile interface .
m_bDonghua: Record whether it is currently in the animation effect , If you don't turn the page in the animation effect ( If not set , Ghosting will appear during fast switching )
m_ptrStackWidget->setCurrentIndex(PreIndex);
m_ptrStackWidget->widget(currentIndex)->show();animation1: Set current page ( Before switching ) Animation effect of face page , You can see startValue and endValue, Is to remove the off screen from the original screen position .
animation2: Set the animation effect about to switch to the interface , You can see startValue and endValue, Is to remove the middle of the screen from the off screen position .
When the animation of the interface is executed at the same time, the sliding effect appears .
void LevelMainWidget::slotBtnPre()
{
if (m_bDonghua) {
return;
}
m_bDonghua = true;
int currentIndex = m_ptrStackWidget->currentIndex();
int windowWidth = m_ptrStackWidget->widget(currentIndex)->width();
int windowHieght = m_ptrStackWidget->widget(currentIndex)->height();
int PreIndex = currentIndex - 1;
if (currentIndex == 0) {
return;
}
m_ptrStackWidget->setCurrentIndex(PreIndex);
m_ptrStackWidget->widget(currentIndex)->show();
QPropertyAnimation* animation1;
QPropertyAnimation* animation2;
QParallelAnimationGroup* group = new QParallelAnimationGroup;
animation1 = new QPropertyAnimation(m_ptrStackWidget->widget(currentIndex),
"geometry");
animation1->setDuration(500);
animation1->setStartValue(QRect(0, 0, windowWidth, windowHieght));
animation1->setEndValue(QRect(windowWidth, 0, windowWidth, windowHieght));
animation2 =
new QPropertyAnimation(m_ptrStackWidget->widget(PreIndex), "geometry");
animation2->setDuration(500);
animation2->setStartValue(
QRect(-windowWidth, 0, windowWidth, windowHieght));
animation2->setEndValue(QRect(0, 0, windowWidth, windowHieght));
group->addAnimation(animation1);
group->addAnimation(animation2);
group->start();
group->setProperty(
"widget", QVariant::fromValue(m_ptrStackWidget->widget(currentIndex)));
connect(group, SIGNAL(finished()), this, SLOT(onAnimationFinished()));
}4、3 Next page slide effect
Get the width and height of the display interface , You need to use the mobile interface .
m_bDonghua: Record whether it is currently in the animation effect , If you don't turn the page in the animation effect ( If not set , Ghosting will appear during fast switching )
m_ptrStackWidget->setCurrentIndex(NextIndex);
m_ptrStackWidget->widget(currentIndex)->show();animation1: Set current page ( Before switching ) Animation effect of face page , You can see startValue and endValue, Is to remove the off screen from the original screen position .
animation2: Set the animation effect about to switch to the interface , You can see startValue and endValue, Is to remove the middle of the screen from the off screen position .
When the animation of the interface is executed at the same time, the sliding effect appears .
void LevelMainWidget::slotBtnNext()
{
if (m_bDonghua) {
return;
}
m_bDonghua = true;
int currentIndex = m_ptrStackWidget->currentIndex();
int windowWidth = m_ptrStackWidget->widget(currentIndex)->width();
int windowHieght = m_ptrStackWidget->widget(currentIndex)->height();
int NextIndex = currentIndex + 1;
if (currentIndex >= m_ptrStackWidget->count()) {
return;
}
m_ptrStackWidget->setCurrentIndex(NextIndex);
m_ptrStackWidget->widget(currentIndex)->show();
QPropertyAnimation* animation1;
QPropertyAnimation* animation2;
QParallelAnimationGroup* group = new QParallelAnimationGroup;
animation1 = new QPropertyAnimation(m_ptrStackWidget->widget(currentIndex),
"geometry");
animation1->setDuration(500);
animation1->setStartValue(QRect(0, 0, windowWidth, windowHieght));
animation1->setEndValue(QRect(-windowWidth, 0, windowWidth, windowHieght));
animation2 =
new QPropertyAnimation(m_ptrStackWidget->widget(NextIndex), "geometry");
animation2->setDuration(500);
animation2->setStartValue(QRect(windowWidth, 0, windowWidth, windowHieght));
animation2->setEndValue(QRect(0, 0, windowWidth, windowHieght));
group->addAnimation(animation1);
group->addAnimation(animation2);
group->start();
group->setProperty(
"widget", QVariant::fromValue(m_ptrStackWidget->widget(currentIndex)));
connect(group, SIGNAL(finished()), this, SLOT(onAnimationFinished()));
}4、4 Animation end processing
After the animation, you need to hide the previous interface , When switching pages, the pointer of the previous page has been saved and sent .
group->setProperty(
"widget", QVariant::fromValue(m_ptrStackWidget->widget(currentIndex)));So at the end of the animation , Get the pointer of the previous page , Then modify its hidden state .
void LevelMainWidget::onAnimationFinished()
{
QParallelAnimationGroup* group = (QParallelAnimationGroup*)sender();
QWidget* widget = group->property("widget").value<QWidget*>();
if (nullptr != widget) {
widget->hide();
}
m_bDonghua = false;
}
5、 ... and 、 Source code address
Source code address : Ah yuan / QT Blog case
6、 ... and 、 Other articles
1. QT The development environment is installed to configure .
2. QT Line drawing board actual combat
3. Play for half an hour QT Desktop system tray ( Message with tray )
4. QT Getting started developing a clock
5. Half an hour to teach you how to play the big wheel game (QT piece )
6. Hand in hand to teach you how to make 【 Line drawing with adsorption effect 】(QT)
7. Hand in hand to teach you to develop - Rolling effect number lottery (QT)
8. 100 Line of code to achieve greedy Snake games
9.C++ Realization 《 Mine clearance 》 game ( Introductory classic )
10. svg Development of image transfer tool
11. Qt Network and communication ( Get local network information )
12. Qt Network and communication (UDP Customer and service )
13. Qt Network and communication (TCP The chat room )
边栏推荐
- 2022养生展,健康展,北京大健康展,健康产业展11月举办
- [release] a tool for testing WebService and database connection - dbtest v1.0
- 建立自己的网站(15)
- 与二值化阈值处理相关的OpenCV函数、方法汇总,便于对比和拿来使用
- 2022 ByteDance daily practice experience (Tiktok)
- 【机器学习的数学基础】(一)线性代数(Linear Algebra)(上+)
- Caché WebSocket
- Build your own website (15)
- 问下各位大佬有用过cdc直接mysql to clickhouse的么
- 神经网络物联网应用技术学什么
猜你喜欢

Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project

A method of using tree LSTM reinforcement learning for connection sequence selection

To sort out messy header files, I use include what you use

Scala basic tutorial -- 15 -- recursion

【uniapp】uniapp开发app在线预览pdf文件

整理混乱的头文件,我用include what you use
![[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0](/img/4e/4154fec22035725d6c7aecd3371b05.jpg)
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0

使用canal配合rocketmq监听mysql的binlog日志

Go microservice (II) - detailed introduction to protobuf

神经网络物联网应用技术就业前景【欢迎补充】
随机推荐
Pb extended DLL development (super chapter) (VII)
Don't just learn Oracle and MySQL!
Use canal and rocketmq to listen to MySQL binlog logs
2021 合肥市信息学竞赛小学组
反射(一)
OpenCV的二值化处理函数threshold()详解
使用canal配合rocketmq监听mysql的binlog日志
技术分享 | 接口测试价值与体系
Shell 编程核心技术《二》
神经网络物联网应用技术学什么
2021 Hefei informatics competition primary school group
2022养生展,健康展,北京大健康展,健康产业展11月举办
Unity给自己的脚本添加类似编辑器扩展的功能案例ContextMenu的使用
利用策略模式优化if代码【策略模式】
To sort out messy header files, I use include what you use
千万不要只学 Oracle、MySQL!
Nebula importer data import practice
大div中有多个div,这些div在同一行显示,溢出后产生滚动条而不换行
2022健康展,北京健博会,中国健康展,大健康展11月13日
性能优化之关键渲染路径