qt 在label上以光标位置进行缩放_缩放|位移|渐变简单动画
生活随笔
收集整理的这篇文章主要介绍了
qt 在label上以光标位置进行缩放_缩放|位移|渐变简单动画
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
本文简单介绍Qt的一些动画效果(缩放,位移,渐变)。
缩放动画
将窗口的geometry(位置,大小)属性作为动画参考实现缩放动画。
代码
w->setWindowTitle(QStringLiteral("缩放动画@Qt君"));
w->resize(320, 240);
QLabel *label = new QLabel(w);
/* 创建一个动画对象 */
QPropertyAnimation *animation = new QPropertyAnimation(label);
/* 设置动画持续时长 */
animation->setDuration(3000);
/* 设置动画目标 */
animation->setTargetObject(label);
/* 设置窗口几何属性(位置,大小)作为动画参考 */
animation->setPropertyName("geometry");
/* 设置动画开始坐标和大小(QRect) */
/* 居中显示数值计算:160 => (320/2 - 100); 120 => (240/2 - 100) */
animation->setStartValue(QRect(160, 120, 0, 0));
/* 设置动画结束坐标和大小(QRect) */
/* 居中显示数值计算(差值是基于开始动画的100/2):160 => (160 - 100/2); 70 => (120 - 100/2) */
animation->setEndValue(QRect(110, 70, 100, 100));
/* 设置循环次数:-1为无限次 */
animation->setLoopCount(-1);
/* 开始动画 */
animation->start();
w->show();
位移动画
将窗口的pos位置作为动画参考实现位移动画。
代码
w->setWindowTitle(QStringLiteral("位移动画@Qt君"));
w->resize(320, 240);
QLabel *label = new QLabel(w);
label->resize(100, 100);
/* 创建一个动画对象 */
QPropertyAnimation *animation = new QPropertyAnimation(label);
/* 设置动画目标 */
animation->setTargetObject(label);
/* 设置窗口的位置作为动画参考 */
animation->setPropertyName("pos");
/* 设置动画持续时长 */
animation->setDuration(3000);
/* 设置动画开始位置 */
animation->setStartValue(QPoint(0, 70));
/* 设置动画结束位置 */
animation->setEndValue(QPoint(220, 70));
/* 设置循环次数:-1为无限次 */
animation->setLoopCount(-1);
/* 开始动画 */
animation->start();
w->show();
渐变动画
使用QGraphicsOpacityEffect配合QPropertyAnimation实现渐变动画。
代码
w->setWindowTitle(QStringLiteral("渐变动画@Qt君"));
w->resize(320, 240);
/* 创建一个不透明效果对象 */
QGraphicsOpacityEffect *opacityEffect = new QGraphicsOpacityEffect;
opacityEffect->setOpacity(1);
QLabel *label = new QLabel(w);
label->setGeometry(110, 70, 100, 100);
/* 设置控件的图形效果 */
label->setGraphicsEffect(opacityEffect);
/* 创建一个动画对象 */
QPropertyAnimation *animation= new QPropertyAnimation(label);
/* 设置动画目标 */
animation->setTargetObject(opacityEffect);
/* 设置窗口的不透明度作为动画参考 */
animation->setPropertyName("opacity");
/* 设置动画持续时长 */
animation->setDuration(3000);
/* 设置动画开始的不透明度 */
animation->setStartValue(0);
/* 设置动画结束的不透明度 */
animation->setEndValue(1);
/* 设置循环次数:-1为无限次 */
animation->setLoopCount(-1);
/* 开始动画 */
animation->start();
w->show();
动画的几点要素
关于更多
- 本文源码例子在公众号聊天界面回复"入群"后在群文件获取。
总结
以上是生活随笔为你收集整理的qt 在label上以光标位置进行缩放_缩放|位移|渐变简单动画的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: java两个长度不同数组_两组数组,长度
- 下一篇: 头部外伤指什么_什么是颅骨缺损?