当前位置:网站首页>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 )
边栏推荐
- 使用FTP
- 《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
- Summary and sorting of 8 pits of redis distributed lock
- Don't just learn Oracle and MySQL!
- Technologie de base de la programmation Shell IV
- 【uniapp】uniapp开发app在线预览pdf文件
- 技术分享 | 接口测试价值与体系
- Caché WebSocket
- 26. Delete the duplicate item C solution in the ordered array
- 指定输出的字符集
猜你喜欢
升级智能开关,“零火版”、“单火”接线方式差异有多大?
小发猫物联网平台搭建与应用模型
神经网络物联网应用技术就业前景【欢迎补充】
Go microservice (II) - detailed introduction to protobuf
Scala basic tutorial -- 15 -- recursion
[release] a tool for testing WebService and database connection - dbtest v1.0
Angry bird design based on unity
redis分布式锁的8大坑总结梳理
Rookie post station management system based on C language
大div中有多个div,这些div在同一行显示,溢出后产生滚动条而不换行
随机推荐
请教一下 flinksql中 除了数据统计结果是状态被保存 数据本身也是状态吗
模板_判断素数_开方 / 六素数法
C language printing exercise
Mxnet implementation of googlenet (parallel connection network)
Go microservice (II) - detailed introduction to protobuf
2022健康展,北京健博会,中国健康展,大健康展11月13日
Shell programming core technology "four"
Shell programming core technology II
Send and receive IBM WebSphere MQ messages
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import 'fmt' func
Angry bird design based on unity
使用SSH
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
物联网应用技术的就业前景和现状
Is Guoyuan futures a regular platform? Is it safe to open an account in Guoyuan futures?
Lex and yacc based lexical analyzer + parser
A method of using tree LSTM reinforcement learning for connection sequence selection
建立自己的网站(15)
升级智能开关,“零火版”、“单火”接线方式差异有多大?
【uniapp】uniapp开发app在线预览pdf文件