当前位置:网站首页>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-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import 'fmt' func
- Using FTP
- Go microservice (II) - detailed introduction to protobuf
- 国元期货是正规平台吗?在国元期货开户安全吗?
- Unity editor extends C to traverse all pictures in folders and subdirectories
- 资料下载 丨首届腾讯技术开放日课程精华!
- 《工作、消费主义和新穷人》的微信读书笔记
- 模板_判断素数_开方 / 六素数法
- Scala basic tutorial -- 18 -- set (2)
- Cache é JSON uses JSON adapters
猜你喜欢

Build your own website (15)

Go microservice (II) - detailed introduction to protobuf

更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
Summary and sorting of 8 pits of redis distributed lock
![[uniapp] uniapp development app online Preview PDF file](/img/11/d640338c626249057f7ad616b55c4f.png)
[uniapp] uniapp development app online Preview PDF file

性能优化之关键渲染路径

LM10丨余弦波动顺势网格策略

神经网络物联网应用技术就业前景【欢迎补充】

每日一题(2022-07-02)——最低加油次数

2022CoCa: Contrastive Captioners are Image-Text Fountion Models
随机推荐
[opencv introduction to mastery 9] opencv video capture, image and video conversion
Detailed explanation of issues related to SSL certificate renewal
Technologie de base de la programmation Shell IV
Basic tutorial of scala -- 16 -- generics
Technology sharing | interface testing value and system
添加命名空间声明
大佬们,求助一下,我用mysql cdc 2.2.1(flink 1.14.5)写入kafka,设置
Shell programming core technology "four"
Pb extended DLL development (super chapter) (VII)
Go微服务(二)——Protobuf详细入门
Using SSH
Build your own website (15)
模板_大整数减法_无论大小关系
Pytest 可视化测试报告之 Allure
MySQL数据库基本操作-DDL | 黑马程序员
SSL证书续费相关问题详解
Torchdrug tutorial
The kth largest element in the array
Perfect JS event delegation
876. Intermediate node of linked list