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

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);
}
http://www.lryc.cn/news/2401408.html

相关文章:

  • 线夹金具测温在线监测装置:电力设备安全运行的“隐形卫士”
  • 《TCP/IP 详解 卷1:协议》第4章:地址解析协议
  • Dify 离线升级操作手册(适用于无外网企业内网环境)
  • Windows下运行Redis并设置为开机自启的服务
  • 网络编程之网络基础
  • Spring AI(11)——SSE传输的MCP服务端
  • 计算机网络备忘录
  • Spring Boot论文翻译防丢失 From船长cap
  • [蓝桥杯]最优包含
  • NuxtJS入门指南:环境安装及报错解决
  • 在java 项目 springboot3.3 中 调用第三方接口(乙方),如何做到幂等操作(调用方为甲方,被调用方为乙方)? 以及啥是幂等操作?
  • 贪心算法应用:集合划分问题详解
  • electron下载文件
  • Neo4j 数据导入:原理、技术、技巧与最佳实践
  • 数论~~~
  • web第十次课后作业--Mybatis的增删改查
  • 贪心算法应用:集合覆盖问题详解
  • BLOB 是用来存“二进制大文件”的字段类型
  • 5.Declare_Query_Checking.ipynb
  • 【知识点】第7章:文件和数据格式化
  • NetSuite Bundle - Dashboard Refresh
  • AI+3D 视觉重塑塑料袋拆垛新范式:迁移科技解锁工业自动化新高度
  • 智慧赋能:移动充电桩的能源供给革命与便捷服务升级
  • 【项目实践】SMBMS(Javaweb版)(三)登出、注册、注销、修改
  • 斐波那契数列------矩阵幂法
  • 【Go语言基础【四】】局部变量、全局变量、形式参数
  • DeepSeek 赋能车路协同:智能交通的破局与重构
  • RabbitMQ 的异步化、解耦和流量削峰三大核心机制
  • Ubuntu 25.10 将默认使用 sudo-rs
  • Maven​​ 和 ​​Gradle​​ 依赖管理的详细说明及示例,涵盖核心概念、配置方法、常见问题解决和工具对比。