当前位置: 首页 > 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.lqws.cn/news/149869.html

相关文章:

  • 【HarmonyOS 5】 社交行业详解以及 开发案例
  • 使用 HTML +JavaScript 从零构建视频帧提取器
  • vue3+ts实现百度地图鼠标绘制多边形
  • Oracle-高频业务表的性能检查
  • 深度解析地质灾害风险普查:RS与GIS技术在泥石流、滑坡灾害中的应用,ArcGIS数据管理、空间数据转换、专题地图制作、DEM分析及实战案例分析
  • Transformer实战——词嵌入技术详解
  • 基于Qt的app开发第十三天
  • Java爬虫技术详解:原理、实现与优势
  • 【设计模式-4.11】行为型——解释器模式
  • JMeter 实现 MQTT 协议压力测试 !
  • MySQL中的部分问题(1)
  • kafka入门学习
  • windows10 php报错
  • 【设计模式】门面/外观模式
  • JVM中的各类引用
  • 【Rust宏编程】Rust有关宏编程底层原理解析与应用实战
  • 5.Declare_Query_Checking.ipynb
  • 群晖NAS套件历史版本资源
  • 【图像处理入门】6. 频域图像处理:傅里叶变换与滤波的奥秘
  • [华为eNSP] OSPF综合实验
  • LeetCode | 滑动窗口的原理及真题解析
  • 【图像处理入门】5. 形态学处理:腐蚀、膨胀与图像的形状雕琢
  • 前端开发面试题总结-CSS篇
  • Neovim - LSP 底层原理,难点配置(二)
  • 【android bluetooth 协议分析 02】【bluetooth hal 层详解 7】【高通蓝牙hal-读流程介绍】
  • 【Nginx】使用 Nginx+Lua 实现基于 IP 的访问频率限制
  • 全球化2.0|云轴科技ZStack助力香港服务机构VMware替代
  • 迷宫与陷阱--bfs+回路+剪枝
  • 极昆仑智慧与数元灵科技达成战略合作
  • DJango项目