Qt之窗口下坠、抖动、透明度

1.下坠效果:

通过计算桌面的宽度、高度,来设置动画的起始值和结束值

void MainWindow::onDropWindow()
{
    QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "geometry");
 
    QDesktopWidget *pDesktopWidget = QApplication::desktop();
    int x = (pDesktopWidget->availableGeometry().width() - width()) / 2;
    int y = (pDesktopWidget->availableGeometry().height() - height()) / 2;
 
    pAnimation->setDuration(1000);
    pAnimation->setStartValue(QRect(x, 0, width(), height()));
    pAnimation->setEndValue(QRect(x, y, width(), height()));
    pAnimation->setEasingCurve(QEasingCurve::OutElastic);
    pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}

2.抖动效果:

获取界面的坐标,然后进行上、下、左、右坐标浮动,通过setKeyValueAt()来设置每一时刻的位置,实现抖动效果

void MainWindow::onShakeWindow()
{
    QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "pos");
    pAnimation->setDuration(500);
    pAnimation->setLoopCount(2);
    pAnimation->setKeyValueAt(0, QPoint(geometry().x() - 3, geometry().y() - 3));
    pAnimation->setKeyValueAt(0.1, QPoint(geometry().x() + 6, geometry().y() + 6));
    pAnimation->setKeyValueAt(0.2, QPoint(geometry().x() - 6, geometry().y() + 6));
    pAnimation->setKeyValueAt(0.3, QPoint(geometry().x() + 6, geometry().y() - 6));
    pAnimation->setKeyValueAt(0.4, QPoint(geometry().x() - 6, geometry().y() - 6));
    pAnimation->setKeyValueAt(0.5, QPoint(geometry().x() + 6, geometry().y() + 6));
    pAnimation->setKeyValueAt(0.6, QPoint(geometry().x() - 6, geometry().y() + 6));
    pAnimation->setKeyValueAt(0.7, QPoint(geometry().x() + 6, geometry().y() - 6));
    pAnimation->setKeyValueAt(0.8, QPoint(geometry().x() - 6, geometry().y() - 6));
    pAnimation->setKeyValueAt(0.9, QPoint(geometry().x() + 6, geometry().y() + 6));
    pAnimation->setKeyValueAt(1, QPoint(geometry().x() - 3, geometry().y() - 3));
    pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}

3.透明度效果:

设置每一时刻的透明度值,动画结束时界面还原(透明度再为1)

void MainWindow::onOpacityWindow()
{
    QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "windowOpacity");
    pAnimation->setDuration(1000);
    pAnimation->setKeyValueAt(0, 1);
    pAnimation->setKeyValueAt(0.5, 0);
    pAnimation->setKeyValueAt(1, 1);
    pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}

https://blog.csdn.net/weixin_33691598/article/details/90524972?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-24&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-24

Qt 中,可以使用 QPropertyAnimation 类来实现子窗口透明度的动画效果。具体步骤如下: 1. 在子窗口类中添加一个成员变量,用于保存透明度的值。 2. 在构造函数中设置子窗口的属性,使其具有窗口透明度,代码如下: ``` setAttribute(Qt::WA_TranslucentBackground); setWindowFlags(Qt::FramelessWindowHint | Qt::Tool); ``` 3. 在子窗口类中添加一个方法,用于设置子窗口透明度,代码如下: ``` void setOpacity(qreal opacity) { m_opacity = opacity; setWindowOpacity(opacity); } ``` 4. 使用 QPropertyAnimation 类创建一个动画对象,将子窗口透明度作为动画属性,代码如下: ``` QPropertyAnimation *animation = new QPropertyAnimation(this, "opacity"); animation->setDuration(1000); animation->setStartValue(0.0); animation->setEndValue(1.0); animation->start(); ``` 5. 启动动画。 完整代码示例: ``` #include <QDialog> #include <QPropertyAnimation> class MyDialog : public QDialog { public: MyDialog(QWidget *parent = 0) : QDialog(parent) , m_opacity(1.0) { setAttribute(Qt::WA_TranslucentBackground); setWindowFlags(Qt::FramelessWindowHint | Qt::Tool); } void setOpacity(qreal opacity) { m_opacity = opacity; setWindowOpacity(opacity); } private: qreal m_opacity; }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MyDialog dialog; dialog.show(); QPropertyAnimation *animation = new QPropertyAnimation(&dialog, "opacity"); animation->setDuration(1000); animation->setStartValue(0.0); animation->setEndValue(1.0); animation->start(); return app.exec(); } ``` 以上代码演示了如何创建一个透明度动画的子窗口,并在启动时开始动画效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值