当前位置: 首页 > 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);
}
http://www.xdnf.cn/news/891469.html

相关文章:

  • LeetCode_LCR 509 斐波拉契
  • 经济学顶刊QJE:构建从非结构化文本数据中挖掘经济规律的新框架!
  • 【QT】qtdesigner中将控件提升为自定义控件后,css设置样式不生效(已解决,图文详情)
  • 实测报告:设备 AI 知识库如何帮助新手快速掌握巡检技巧?
  • 在嵌入式中C语言中static修饰的变量常量和字符串常量存储位置
  • 总结vxe-grid的一些用法
  • 精度分析方法-不确定度
  • [蓝桥杯]三体攻击
  • MySQL的并发事务问题及事务隔离级别
  • 12V降5V12A大功率WD5030A,充电器、便携式设备、网络及工业领域的理想选择
  • 大语言模型评测体系全解析(中篇):专项能力评测与行业垂直场景
  • Mysql莫名奇妙重启
  • 实现单例模式的常见方式
  • Redis Set集合命令、内部编码及应用场景(详细)
  • GC1809:高性能音频接收与转换芯片
  • Python Day42 学习(日志Day9复习)
  • AI智能推荐实战之RunnableParallel并行链
  • .Net Framework 4/C# System.IO 命名空间(文件的输入输出)
  • 深度学习之模型压缩三驾马车:基于ResNet18的模型剪枝实战(2)
  • 箭头函数和普通函数的this指向
  • BLE中心与外围设备MTU协商过程详解
  • 炫云:为驱动数字视觉产业升级保驾护航
  • 【设计模式-4.11】行为型——解释器模式
  • centos实现SSH远程登录
  • 分布式一致性原理及一致性协议
  • AI数字人小程序开发,重塑商业服务新模式
  • 6个月Python学习计划 Day 15 - 函数式编程、高阶函数、生成器/迭代器
  • 分析vban的utlis中的helper方法(1)——数组
  • 【技术笔记】AI Agent 项目 SUNA 部署:MSYS2 环境中 Python 版本从 3.12 降级至 3.11 的实操指南
  • place 布局管理器