一、效果展示
二、源码分享
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);
}