《QT实用小工具·五十四》果冻弹出效果的动画按钮

1、概述
源码放在文章末尾

该项目实现动画按钮,鼠标放在按钮上可以弹性拉出的三个按钮,使用贝塞尔曲线实现,项目demo显示如下所示:

项目部分代码如下所示:

cpp 复制代码
#ifndef WATERCIRCLEBUTTON_H
#define WATERCIRCLEBUTTON_H

#include <QObject>
#include <QWidget>
#include "interactivebuttonbase.h"

class WaterCircleButton : public InteractiveButtonBase
{
public:
    WaterCircleButton(QWidget* parent = nullptr);
    WaterCircleButton(QIcon icon, QWidget* parent = nullptr);
    WaterCircleButton(QPixmap pixmap, QWidget* parent = nullptr);

protected:
    void enterEvent(QEvent* event) override;
    void leaveEvent(QEvent* event) override;
    void mousePressEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
    void resizeEvent(QResizeEvent* event) override;

    QPainterPath getBgPainterPath() override;
    QPainterPath getWaterPainterPath(Water water) override;

    void simulateStatePress(bool s = true);
    bool inArea(QPoint point) override;

protected:
    QPoint center_pos;
    bool in_circle;
    int radius;
};

#endif // WATERCIRCLEBUTTON_H
cpp 复制代码
#include "watercirclebutton.h"

WaterCircleButton::WaterCircleButton(QWidget* parent) : InteractiveButtonBase (parent), in_circle(false), radius(16)
{

}

WaterCircleButton::WaterCircleButton(QIcon icon, QWidget *parent) : InteractiveButtonBase (icon, parent), in_circle(false), radius(16)
{

}

WaterCircleButton::WaterCircleButton(QPixmap pixmap, QWidget *parent) : InteractiveButtonBase (pixmap, parent), in_circle(false), radius(16)
{

}

void WaterCircleButton::enterEvent(QEvent *event)
{

}

void WaterCircleButton::leaveEvent(QEvent *event)
{
    if (in_circle && !pressing && !inArea(mapFromGlobal(QCursor::pos())))
    {
        in_circle = false;
        InteractiveButtonBase::leaveEvent(event);
    }
}

void WaterCircleButton::mousePressEvent(QMouseEvent *event)
{
    if (in_circle || (!hovering && inArea(event->pos())))
        return InteractiveButtonBase::mousePressEvent(event);
}

void WaterCircleButton::mouseReleaseEvent(QMouseEvent *event)
{
    if (pressing)
    {
        InteractiveButtonBase::mouseReleaseEvent(event);

        if (leave_after_clicked || (!inArea(event->pos()) && !pressing)) // 鼠标移出
        {
            in_circle = false;
            InteractiveButtonBase::leaveEvent(nullptr);
        }
    }
}

void WaterCircleButton::mouseMoveEvent(QMouseEvent *event)
{
    bool is_in = inArea(event->pos());

    if (is_in && !in_circle)// 鼠标移入
    {
        in_circle = true;
        InteractiveButtonBase::enterEvent(nullptr);
    }
    else if (!is_in && in_circle && !pressing) // 鼠标移出
    {
        in_circle = false;
        InteractiveButtonBase::leaveEvent(nullptr);
    }

    if (in_circle)
        InteractiveButtonBase::mouseMoveEvent(event);
}

void WaterCircleButton::resizeEvent(QResizeEvent *event)
{
    center_pos = geometry().center() - geometry().topLeft();
    radius = min(size().width(), size().height())/ 2;

    return InteractiveButtonBase::resizeEvent(event);
}

QPainterPath WaterCircleButton::getBgPainterPath()
{
    QPainterPath path;
    int w = size().width(), h = size().height();
    QRect rect(w/2-radius, h/2-radius, radius*2, radius*2);
    path.addEllipse(rect);
    return path;
}

QPainterPath WaterCircleButton::getWaterPainterPath(InteractiveButtonBase::Water water)
{
    QPainterPath path = InteractiveButtonBase::getWaterPainterPath(water) & getBgPainterPath();
    return path;
}

void WaterCircleButton::simulateStatePress(bool s)
{
    in_circle = true;
    InteractiveButtonBase::simulateStatePress(s);
    in_circle = false;
}

bool WaterCircleButton::inArea(QPoint point)
{
    return (point - center_pos).manhattanLength() <= radius;
}

源码下载

相关推荐
Amor风信子1 分钟前
华为OD机试真题---战场索敌
java·开发语言·算法·华为od·华为
计算机内卷的N天16 分钟前
UI样式表(悬停hover状态样式和按下pressed)
qt
old_power35 分钟前
【PCL】Segmentation 模块—— 基于图割算法的点云分割(Min-Cut Based Segmentation)
c++·算法·计算机视觉·3d
fmdpenny36 分钟前
Vue3初学之商品的增,删,改功能
开发语言·javascript·vue.js
涛ing1 小时前
21. C语言 `typedef`:类型重命名
linux·c语言·开发语言·c++·vscode·算法·visual studio
等一场春雨1 小时前
Java设计模式 十四 行为型模式 (Behavioral Patterns)
java·开发语言·设计模式
黄金小码农2 小时前
C语言二级 2025/1/20 周一
c语言·开发语言·算法
萧若岚2 小时前
Elixir语言的Web开发
开发语言·后端·golang
wave_sky2 小时前
解决使用code命令时的bash: code: command not found问题
开发语言·bash
PaLu-LI2 小时前
ORB-SLAM2源码学习:Initializer.cc⑧: Initializer::CheckRT检验三角化结果
c++·人工智能·opencv·学习·ubuntu·计算机视觉