【Qt】实现界面抖动

简介: Qt 实现类似 QQ 界面震一下的代码

Qt 中实现某个界面的震动,界面可以继承 QWidget, QDialog, QMainWindow, 都可以

  • QWidget 单独出现显示为一个窗口,若在其他 QDialog 或 QMainWindow 中,则为界面的一个控件,会和父类窗口合为一体。

  • QDialog 为对话框,右上角窗口只显示 ? 和 x 关闭

  • QMainWindow 为程序主界面,右上角为完整的三个键,最小化,最大化和关闭,界面包含主界面(QCentralWidget),菜单栏 (QMenuBar),状态栏 (QStatusBar), 工具栏...

这里以 QWidget 为示例,QWidget 中添加槽函数

class Widget : public QWidget
{
   
//...
public slots:
  void slotVibrate();
};

源文件中需要使用 <QPropertyAnimation> 库,包含对应头文件

#include<QPropertyAnimation> // 包含头文件

需要使用到
setDuration 设置持续时长
setLoopCount 设置循环次数

QPropertyAnimation *animation = new QPropertyAnimation(this, "viber");
animation->setDuration(500);
animation->setLoopCount(2);

主要需要使用 setKeyValueAt
第一个参数为 0-1 ,0 为初始时间, 1 为结束时间, 0.1-0.9 为中间的时间。
第二个参数为值,我们这里使用 QPoint 来表示坐标。
例:

// 设置初始值为 50
animation->setKeyValueAt(0, 50);
// 中间时刻时,值变化到 20
animation->setKeyValueAt(0.5, 20);
// 从 20 到结束时,值变为 60
animation->setKeyValueAt(1, 60);

完整代码为:

#include<QPropertyAnimation>
//...
void Widget::slotVibrate()
{
   
    QPropertyAnimation *animation = new QPropertyAnimation(this, "viber");
    animation->setDuration(500);
    animation->setLoopCount(2);

    animation->setKeyValueAt(0  , QPoint(geometry().x() - 3, geometry().y() - 3));
    animation->setKeyValueAt(0.1, QPoint(geometry().x() + 6, geometry().y() + 6));
    animation->setKeyValueAt(0.2, QPoint(geometry().x() - 6, geometry().y() + 6));
    animation->setKeyValueAt(0.3, QPoint(geometry().x() + 6, geometry().y() - 6));
    animation->setKeyValueAt(0.4, QPoint(geometry().x() - 6, geometry().y() - 6));
    animation->setKeyValueAt(0.5, QPoint(geometry().x() + 6, geometry().y() + 6));
    animation->setKeyValueAt(0.6, QPoint(geometry().x() - 6, geometry().y() + 6));
    animation->setKeyValueAt(0.7, QPoint(geometry().x() + 6, geometry().y() - 6));
    animation->setKeyValueAt(0.8, QPoint(geometry().x() - 6, geometry().y() - 6));
    animation->setKeyValueAt(0.9, QPoint(geometry().x() + 6, geometry().y() + 6));
    animation->setKeyValueAt(1  , QPoint(geometry().x() - 3, geometry().y() - 3));

    animation->start(QAbstractAnimation::DeleteWhenStopped); // 设置震动结束删除
}
目录
相关文章
|
4月前
|
数据可视化 API vr&ar
探索Qt 3D之旅:从基础到实战,打造引人入胜的三维界面与应用
探索Qt 3D之旅:从基础到实战,打造引人入胜的三维界面与应用
711 2
|
4月前
|
编解码 并行计算 Java
QT界面中实现视频帧显示的多种方法及应用(二)
QT界面中实现视频帧显示的多种方法及应用
706 0
|
4月前
|
存储 测试技术 UED
Qt中实现界面回放的艺术:从理论到代码“ (“The Art of Implementing UI Playback in Qt: From Theory to Code
Qt中实现界面回放的艺术:从理论到代码“ (“The Art of Implementing UI Playback in Qt: From Theory to Code
115 1
|
1月前
|
监控 C++ 容器
【qt】MDI多文档界面开发
【qt】MDI多文档界面开发
47 0
|
4月前
|
编译器
QT creator开发环境下 界面更改后运行程序不能实时更新或者在源文件添加该控件后无法编译的问题
在使用QT Creator开发界面的过程中,偶尔会出现添加控件后,运行程序后,界面控件无法更新的情况,或者在源文件使用该控件却出现无法编译的情况,使用QT Creator 4.8.2也会出现这个情况,也不知道这种情况会不会在以后有所改善。
178 0
|
4月前
|
编解码 C++
Qt第一课 第一个ui界面
Qt第一课 第一个ui界面
59 2
|
4月前
|
区块链
【qt】最快的开发界面效率——混合编程3
【qt】最快的开发界面效率——混合编程
77 1
|
4月前
【qt】最快的开发界面效率——混合编程2
【qt】最快的开发界面效率——混合编程
61 1
|
4月前
【qt】设计器实现界面
【qt】设计器实现界面
45 1
|
4月前
|
搜索推荐
【qt】自定义界面类
【qt】自定义界面类
40 0