前言
基于QPushButton实现的悬浮按钮功能,并且加入了一些动画,目前实现了基本的功能,满足可用,其他功能细节正在完善中。实现思路计算坐标然后绘制,尽可能减少贴图的使用。
效果图如下,代码自取,请放心食用。有好的想法建议欢迎留言交流,会不定期回复。
效果图
源码
懒得写描述了,自己看吧,直接拿去用也不是不行。
fr_floating_action_button.h
#ifndef FR_FLOATING_ACTION_BUTTON_H
#define FR_FLOATING_ACTION_BUTTON_H
#include <stdint.h>
#include <qpushbutton.h>
#include <qanimationgroup.h>
#include <qpropertyanimation.h>
class FrFloatingActionButton : public QPushButton {
Q_OBJECT
public:
explicit FrFloatingActionButton(QWidget *parent, uint8_t index);
virtual ~FrFloatingActionButton();
void set_path(const QString &path);
void set_text(const QString &text);
void set_icon(const QString &icon);
void Show(bool is);
void ReCalAnimation();
protected:
virtual void paintEvent(QPaintEvent *event);
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
virtual void enterEvent(QEnterEvent *event);
#else
virtual void enterEvent(QEvent *event);
#endif
virtual void leaveEvent(QEvent *event);
private:
void draw_border(QPainter& painter);
void draw_text(QPainter& painter);
private slots:
void transpond_click_signal();
signals:
void Clicked(QString text, QString path, quint8 index);
void ButtonActived(quint8 index, bool is);
private:
const uint8_t index_;
QString path_;
QString text_;
QString icon_;
QPropertyAnimation* zoom_in_;
QPropertyAnimation* zoom_out_;
QPropertyAnimation* float_out_;
QPropertyAnimation* float_back_;
};
#endif // FR_FLOATING_ACTION_BUTTON_H
fr_floating_action_button.cpp
#include "fr_floating_action_button.h"
#include <math.h>
#include <qevent.h>
#include <qpainter.h>
#define DURATION 100
FrFloatingActionButton::FrFloatingActionButton(QWidget *parent, uint8_t index)
: QPushButton(parent)
, index_(index)
, zoom_in_(new QPropertyAnimation(this, "geometry", this))
, zoom_out_(new QPropertyAnimation(this, "geometry", this))
, float_out_(new QPropertyAnimation(this, "pos", this))
, float_back_(new QPropertyAnimation(this, "pos", this)) {
connect(this, &FrFloatingActionButton::clicked, this, &FrFloatingActionButton::transpond_click_signal);
connect(zoom_out_, &QPropertyAnimation::finished, this, &FrFloatingActionButton::hide);
}
FrFloatingActionButton::~FrFloatingActionButton() {
}
void FrFloatingActionButton::set_path(const QString &path) {
path_ = path;
}
void FrFloatingActionButton::set_text(const QString &text) {
text_ = text;
}
void FrFloatingActionButton::set_icon(const QString &icon) {
icon_ = icon;
}
void FrFloatingActionButton::Show(bool is) {
if (index_ == 0) {
setVisible(is);
return;
}
if (path_.isEmpty()) {
setVisible(false);
return;
}
if (is) {
if (!isVisible()) {
setVisible(is);
zoom_in_->start();
}
} else {
zoom_out_->start();
}
}
void FrFloatingActionButton::ReCalAnimation() {
zoom_in_->setStartValue(QRect(pos() * 1.25 , size() / 2.0));
zoom_in_->setEndValue(QRect(pos(), size()));
zoom_in_->setDuration(DURATION);
zoom_out_->setStartValue(QRect(pos(), size()));
zoom_out_->setEndValue(QRect(pos() * 1.25 , size() / 2.0))