Qt:悬浮按钮

Qt:悬浮按钮

前言

基于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))
Qt是一个跨平台的应用程序开发框架,提供了丰富的库和工具,供开发者使用来创建各种类型的应用程序。VLC是一个开源的媒体播放器,支持多种音频和视频格式。 悬浮按钮是指在界面上以浮动的方式展示出来的按钮,通常用于提供快捷的操作入口或者对某个功能进行开关操作。在Qt中,我们可以使用QToolButton或QPushButton实现悬浮按钮的效果。 首先,我们需要创建一个新的窗口或者对话框来容纳悬浮按钮。然后,我们可以使用QToolButton或QPushButton来创建一个按钮,并且设置其父对象为我们的窗口或者对话框。 悬浮按钮的特性可以通过设置按钮的属性来实现。比如,我们可以设置按钮的大小、位置、文本、图标、样式等。此外,还可以为按钮添加相应的信号和槽函数,以实现按钮被点击时的响应。 对于Qt中的悬浮按钮来说,可以使用setToolButtonStyle函数来设置按钮的样式,以实现悬浮按钮的外观效果。Qt还提供了一些现成的样式,可以根据需要选择合适的样式,或者自定义样式来满足我们的需求。 对于VLC的悬浮按钮来说,则需要借助VLC的API来实现。VLC提供了丰富的接口,供开发者使用来控制播放器的各种功能。我们可以通过使用VLC的API来创建悬浮按钮,并且设置按钮的行为和外观。 总之,Qt和VLC都提供了丰富的功能来实现悬浮按钮的效果。通过合理使用这些框架和工具,我们可以轻松地在应用程序中添加悬浮按钮,提供更好的用户体验。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值