当前位置: 首页 > news >正文

Qt实现一个悬浮工具箱源码分享

一、效果展示

在这里插入图片描述

二、源码分享

hoverToolboxWidget.h

#ifndef HOVERTOOLBOXWIDGET_H
#define HOVERTOOLBOXWIDGET_H#include <QWidget>
#include <QMouseEvent>
#include <QPropertyAnimation>
#include <QStyleOption>
#include <QPainter>namespace Ui {
class HoverToolboxWidget;
}class HoverToolboxWidget : public QWidget
{Q_OBJECT
signals:void btnClickSlot(QString fun);
public:explicit HoverToolboxWidget(QWidget *parent = nullptr);~HoverToolboxWidget();
protected:void paintEvent(QPaintEvent *event) override;bool eventFilter(QObject *obj,QEvent *event) override;
private:void controlInit();void extand();
private:Ui::HoverToolboxWidget *ui;bool isDragging = false,isExtending = false;QPointF dragPos;QPropertyAnimation *amplifyAnimation,*leaveAnimation;static const uint8_t cellSize = 75;
};#endif // HOVERTOOLBOXWIDGET_H

hoverToolboxWidget.cpp

#include "hoverToolboxWidget.h"
#include "ui_hoverToolboxWidget.h"HoverToolboxWidget::HoverToolboxWidget(QWidget *parent): QWidget(parent), ui(new Ui::HoverToolboxWidget)
{ui->setupUi(this);this->controlInit();
}HoverToolboxWidget::~HoverToolboxWidget()
{delete ui;
}
void HoverToolboxWidget::controlInit()
{setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);setAttribute(Qt::WA_TranslucentBackground, true);this->ui->labelImage->installEventFilter(this);this->ui->labelImage->setMouseTracking(true);this->ui->frame->installEventFilter(this);this->ui->frame->setMouseTracking(true);this->ui->pushButton1->hide();this->ui->pushButton2->hide();this->ui->pushButton3->hide();this->ui->pushButton4->hide();this->ui->pushButton5->hide();this->ui->pushButton6->hide();this->ui->pushButton7->hide();this->ui->pushButton8->hide();this->resize(cellSize,cellSize);this->setAttribute(Qt::WA_StyledBackground, true);connect(this->ui->pushButton1,&QPushButton::clicked,this,[=](){emit btnClickSlot("openRootDir");});connect(this->ui->pushButton1,&QPushButton::clicked,this,[=](){emit btnClickSlot("screenShot");});
}void HoverToolboxWidget::extand()
{if(!isExtending){auto pos = this->pos();int x = pos.x()-cellSize-this->ui->frame->layout()->spacing();int y = pos.y()-cellSize-this->ui->frame->layout()->spacing();int wh = cellSize*3 + this->ui->frame->layout()->spacing()*2 + this->ui->labelImage->pos().x()*2;this->setGeometry(x,y,wh,wh);this->ui->pushButton1->show();this->ui->pushButton2->show();this->ui->pushButton3->show();this->ui->pushButton4->show();this->ui->pushButton5->show();this->ui->pushButton6->show();this->ui->pushButton7->show();this->ui->pushButton8->show();//判断有没有超出边界int jx = this->pos().x();int jy = this->pos().y();if(jx < 0)jx = 0;if(jx+this->width() > this->parentWidget()->width())jx = this->parentWidget()->width()-this->width();if(jy < 0)jy = 0;if(jy + this->height() > this->parentWidget()->height())jy = this->parentWidget()->height() - this->height();this->move(jx,jy);isExtending = true;this->ui->labelImage->setPixmap(QPixmap(":/image/image/toolboxOpen.svg"));}
}void HoverToolboxWidget::paintEvent(QPaintEvent *event)
{Q_UNUSED(event);QStyleOption option;option.initFrom(this);QPainter painter(this);style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
}bool HoverToolboxWidget::eventFilter(QObject *obj, QEvent *event)
{if(obj == this->ui->labelImage){if(event->type() == QEvent::MouseButtonPress){QMouseEvent *ev = dynamic_cast<QMouseEvent *>(event);if (ev->button() == Qt::LeftButton){isDragging = true;dragPos = ev->globalPosition() - frameGeometry().topLeft();}if (ev->button() == Qt::RightButton){this->extand();}}else if(event->type() == QEvent::MouseMove){QMouseEvent *ev = dynamic_cast<QMouseEvent *>(event);if (isDragging){QPointF movePoint = ev->globalPosition() - dragPos;int x = movePoint.x();int y = movePoint.y();if(x < 0)x = 0;if((x+this->width()) > this->parentWidget()->width())x = this->parentWidget()->width() - this->width();if(y < 0)y = 0;if((y+this->height()) > this->parentWidget()->height())y = this->parentWidget()->height() - this->height();this->move(x,y);}}else if(event->type() == QEvent::MouseButtonRelease){QMouseEvent *ev = dynamic_cast<QMouseEvent *>(event);if (ev->button() == Qt::LeftButton) {isDragging = false;}}else if(event->type() == QEvent::MouseButtonDblClick){this->extand();}}else if(obj == this->ui->frame){if(event->type() == QEvent::Leave){if(isExtending){isExtending = false;auto pos = this->pos();this->ui->pushButton1->hide();this->ui->pushButton2->hide();this->ui->pushButton3->hide();this->ui->pushButton4->hide();this->ui->pushButton5->hide();this->ui->pushButton6->hide();this->ui->pushButton7->hide();this->ui->pushButton8->hide();int x = pos.x()+cellSize + this->ui->frame->layout()->spacing();int y = pos.y()+cellSize + this->ui->frame->layout()->spacing();this->setGeometry(x,y,cellSize,cellSize);this->ui->labelImage->setPixmap(QPixmap(":/image/image/toolboxClose.svg"));}}}return QWidget::eventFilter(obj,event);
}

相关文章:

  • 当机械音色遇见抒情诗:IndexTTS与CosyVoice的中文语音对决
  • PS如何傻瓜式扣图、图片编辑、图片合成
  • springcloud openfeign 偶现 Caused by: java.net.UnknownHostException
  • 强化学习原理入门-2马尔科夫决策过程
  • jar包如何引入
  • C++算法动态规划1
  • Odoo电子邮件使用配置指南
  • CppCon 2015 学习:Comparison is not simple, but it can be simpler.
  • 008-libb64 你有多理解base64?-C++开源库108杰
  • AppTrace技术全景:开发者视角下的工具链与实践经验
  • GPU 图形计算综述 (三):可编程管线 (Programmable Pipeline)
  • 数据结构:递归:泰勒展开式(Taylor Series Expansion)
  • 架构师级考验!飞算 JavaAI 炫技赛:AI 辅助编程解决老项目难题
  • 单精度浮点数值 和 双精度浮点数值
  • 嵌入式学习之系统编程(十)网络编程之TCP传输控制协议
  • TDengine 开发指南—— UDF函数
  • Web 架构相关文章目录(持续更新中)
  • YAML在自动化测试中的三大核心作用
  • RADIUS-管理员获取共享密钥
  • 拆装与维修汇总帖