QT界面切换特效

转载自:hezf_hero:https://blog.csdn.net/hezf_hero/article/details/50187483

应用场景

在开发桌面应用的时候,经常性的会在几个界面之间切换
可以是局部的,也可以是整个界面
以前我总是利用hide和show来完成
但是很缺乏动态的美感,用户在使用的时候体验不好
今天就来解决这个问题


下面进入正题:
QPropertyAnimation
在QT中使用这个类可以很容易的设置一般的动画

淡入淡出

    QPropertyAnimation *animation = new QPropertyAnimation(&w,"windowOpacity");
    animation->setDuration(1000);
    animation->setStartValue(0);
    animation->setEndValue(1);
    animation->start();

上面这段代码首先绑定一个widget,这个动画将接收widget的图形部分
然后设置整个动画的时长为1000ms
setStartValue和setEndValue这里设置了从0到1
效果就是谈入
start以后开始慢慢的出现
当然也可以设置从1到0,效果自然就是淡出了
效果是这样的
淡入淡出

界面平移

    QLabel *label = new QLabel(this);
    label->resize(this->centralWidget()->size());
    label->setPixmap(this->centralWidget()->grab());
    label->show();
    QPropertyAnimation *animation= new QPropertyAnimation(label,"geometry");
    animation->setDuration(1000);
    animation->setStartValue(this->centralWidget()->geometry());
    animation->setEndValue(QRect(-this->centralWidget()->width(), 0, this->centralWidget()->width(), this->centralWidget()->height()));
    animation->start();

上面的代码和淡入淡出不同
我使用了一个label来获取整个界面的的大小和图像
然后使用QPropertyAnimation 绑定
重点来了,这次我在setStartValue和setEndValue的时候不是一般的整数,而是使用的整个集合图形QRect
关于QRect简单介绍下
QRect用来表示一个矩形的位置和大小
具体地说就是一个QPoint(左上角的点)、宽度和高度
有了这几个参数,这个矩形的位置和大小就统一了
下图来自官方文档
QRect
所以我们实际上我们setStartValue和setEndValue了一个矩形的位置和大小
这样,如果我们的大小不变,只改变QPoint的横坐标,那么平移的效果就出来了
如下图,向左侧移动
这里写图片描述
向上或者向下也是可以的
这里写图片描述
上面两种情况是这个界面移走,下个界面不动
那么我想让这个界面移走的时候下个界面不是不动,而是跟着移动进来呢?
那么我们就得同时拥有两个动画:

  1. 移出动画
  2. 移入动画
    而且要动作一致才会有好的效果
    QLabel *label = new QLabel(this);
    label->resize(this->centralWidget()->size());
    label->setPixmap(this->centralWidget()->grab());
    label->show();

    QPropertyAnimation *animation= new QPropertyAnimation(label,"geometry");
    animation->setDuration(500);
    animation->setStartValue(this->centralWidget()->geometry());
    animation->setEndValue(QRect(this->centralWidget()->width(), 0, this->centralWidget()->width(), this->centralWidget()->height()));

    QPropertyAnimation *animation1= new QPropertyAnimation(this->centralWidget(),"geometry");
    animation1->setDuration(500);
    animation1->setStartValue(QRect(-this->centralWidget()->width(), 0, this->centralWidget()->width(), this->centralWidget()->height()));
    animation1->setEndValue(this->centralWidget()->geometry());

    QParallelAnimationGroup *group = new QParallelAnimationGroup;
    group->addAnimation(animation);
    group->addAnimation(animation1);
    group->start();

在上面我们使用了QParallelAnimationGroup 这个类
它就像容器一样可以装载很多个动画,然后同时让它们开始
达到的效果就是这样的:
这里写图片描述
QPropertyAnimation 同时还支持线性插值操作

animation->setKeyValueAt(0, QRect(0, 0, 00, 00));  
animation->setKeyValueAt(0.4, QRect(20, 250, 20, 30));  
animation->setKeyValueAt(0.8, QRect(100, 250, 20, 30));  
animation->setKeyValueAt(1, QRect(250, 250, 100, 30));  
animation->setEndValue(QRect(250, 250, 100, 30)); 

动画会在前40%由QRect(0, 0, 00, 00)移动改变到QRect(20, 250, 20, 30)
然后再40%由QRect(20, 250, 20, 30)移动改变到QRect(100, 250, 20, 30)
最后到QRect(250, 250, 100, 30)
是不是很方便?

回弹效果

QEasingCurve 类还提供了很多种线性插值,下面是使用方法

    animation->setStartValue(this->centralWidget()->geometry());
    animation->setEndValue(QRect(-this->centralWidget()->width(), 0, this->centralWidget()->width(), this->centralWidget()->height()));
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();

下图就是OutBounce的线性插值,会有一个回弹的效果

这里写图片描述

是不是很棒?还有很多的值可以一一试验,下面是官方文档截图
这里写图片描述


  • 18
    点赞
  • 127
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Qt界面中实现反转切换效果可以通过使用QPropertyAnimation类和QGraphicsOpacityEffect类来实现。首先,我们需要在界面上创建一个QWidget或QFrame作为容器,然后为其添加一个QGraphicsOpacityEffect类的实例,用于实现透明度的动画效果。 接下来,我们创建一个QPropertyAnimation类的实例,将其与QGraphicsOpacityEffect类的透明度属性关联起来。然后设置动画的起始和结束值,以及动画的持续时间。 接着,通过调用QPropertyAnimation类的start()方法启动动画效果。当动画完成后,可以根据需要执行其他操作,比如切换界面或显示其他元素。 以下是一个简单的示例代码: ```cpp #include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; QHBoxLayout layout(&window); QFrame frame; QGraphicsOpacityEffect effect(&frame); frame.setGraphicsEffect(&effect); QLabel label1("Label 1", &frame); QLabel label2("Label 2", &frame); label1.setAlignment(Qt::AlignCenter); label2.setAlignment(Qt::AlignCenter); layout.addWidget(&frame); layout.addWidget(&label1); layout.addWidget(&label2); QPropertyAnimation animation(&effect, "opacity"); animation.setStartValue(0.0); animation.setEndValue(1.0); animation.setDuration(1000); QTimer::singleShot(0, [&]() { animation.start(); }); QObject::connect(&animation, &QPropertyAnimation::finished, [&]() { // 反转切换完成后的操作 }); window.show(); return app.exec(); } ``` 在上面的示例中,我们创建了一个包含两个标签Label 1和Label 2的帧,并且通过动画效果实现了反转切换的效果。你可以根据需要修改代码,添加更多的元素或自定义动画效果的参数。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值