大家好,又见面了,我是你们的朋友全栈君。
首先需要有一个按钮用来显示和隐藏列表
m_button = new QPushButton(QStringLiteral("隐藏"),parent);
m_button->resize(35,35);
当点击按钮的时候隐藏或显示列表
connect(m_button,&QPushButton::clicked,this,&HideShowListView::clickButton);
void HideShowListView::clickButton()
{
if(m_list->isHidden())
{
m_list->show();
m_button->setText(QStringLiteral("隐藏"));
}
else
{
m_list->hide();
m_button->setText(QStringLiteral("显示"));
}
}
列表显示的时候按钮位于列表左侧,隐藏的时候位于窗口右边
bool HideShowListView::eventFilter(QObject *watched, QEvent *event)
{
if(m_list->isHidden())
{
if(event->type() == QEvent::Resize || event->type() == QEvent::Hide)
{
int x = m_parent->width()-5-m_button->width();
int y = (m_parent->height()-m_button->height())/2;
m_button->move(x,y);
return false;
}
}
else
{
if(event->type() == QEvent::Resize|| event->type() == QEvent::Show)
{
int x = m_list->pos().x()-m_button->width();
int y = m_list->pos().y()+(m_list->height()-m_button->height())/2;
m_button->move(x,y);
return false;
}
}
return QObject::eventFilter(watched,event);
}
为鼠标添加透明度动画
QGraphicsOpacityEffect * effect = new QGraphicsOpacityEffect();
m_button->setGraphicsEffect(effect);
effect->setOpacity(0);
m_animation = new QPropertyAnimation(effect,"opacity",this);
m_animation->setDuration(100);
鼠标进入窗口的时候按钮需要显示,移动至窗口外面的时候需要隐藏
鼠标在窗口停止移动超过1.5秒,隐藏鼠标
鼠标移动的时候,显示鼠标
if(event->type() == QEvent::HoverEnter && watched == m_parent)
{
m_animation->setStartValue(0);
m_animation->setEndValue(0.99);
m_animation->start();
return false;
}
else if(event->type() == QEvent::HoverLeave && watched == m_parent)
{
m_animation->setStartValue(1.0);
m_animation->setEndValue(0);
m_animation->start();
return false;
}
if(event->type() == QEvent::HoverMove)
{
if(m_timerId!=-1)
{
killTimer(m_timerId);
}
m_timerId = this->startTimer(1500);
m_animation->setStartValue(m_animation->currentValue());
m_animation->setEndValue(0.99);
m_animation->start();
}
void HideShowListView::timerEvent(QTimerEvent *event)
{
if(event->timerId() == m_timerId)
{
m_animation->setStartValue(1.0);
m_animation->setEndValue(0);
m_animation->start();
killTimer(m_timerId);
m_timerId=-1;
}
}
完整代码:
#ifndef HIDESHOWLISTVIEW_H
#define HIDESHOWLISTVIEW_H
#include <QObject>
#include <QSize>
QT_BEGIN_NAMESPACE
class QPushButton;
class QPropertyAnimation;
QT_END_NAMESPACE
class HideShowListView : public QObject
{
Q_OBJECT
public:
explicit HideShowListView(QWidget*list, QWidget *parent = nullptr);
// QObject interface
public:
bool eventFilter(QObject *watched, QEvent *event);
private slots:
void clickButton();
private:
int m_timerId=-1;
QWidget*m_list;
QPushButton *m_button;
QWidget *m_parent;
QPropertyAnimation *m_animation;
// QObject interface
protected:
void timerEvent(QTimerEvent *event);
};
#endif // HIDESHOWLISTVIEW_H
#include "hideshowlistview.h"
#include <QEvent>
#include <QPushButton>
#include <QStringLiteral>
#include <QWidget>
#include <QDebug>
#include <QPropertyAnimation>
#include <QGraphicsOpacityEffect>
HideShowListView::HideShowListView(QWidget *list, QWidget *parent) : QObject(parent)
{
m_list = list;
m_parent=parent;
m_button = new QPushButton(QStringLiteral("隐藏"),parent);
m_button->resize(35,35);
QGraphicsOpacityEffect * effect = new QGraphicsOpacityEffect();
m_button->setGraphicsEffect(effect);
effect->setOpacity(0);
m_animation = new QPropertyAnimation(effect,"opacity",this);
m_animation->setDuration(100);
list->installEventFilter(this);
parent->installEventFilter(this);
connect(m_button,&QPushButton::clicked,this,&HideShowListView::clickButton);
}
bool HideShowListView::eventFilter(QObject *watched, QEvent *event)
{
if(m_list->isHidden())
{
if(event->type() == QEvent::Resize || event->type() == QEvent::Hide)
{
int x = m_parent->width()-5-m_button->width();
int y = (m_parent->height()-m_button->height())/2;
m_button->move(x,y);
return false;
}
}
else
{
if(event->type() == QEvent::Resize|| event->type() == QEvent::Show)
{
int x = m_list->pos().x()-m_button->width();
int y = m_list->pos().y()+(m_list->height()-m_button->height())/2;
m_button->move(x,y);
return false;
}
}
if(event->type() == QEvent::HoverEnter && watched == m_parent)
{
m_animation->setStartValue(0);
m_animation->setEndValue(0.99);
m_animation->start();
return false;
}
else if(event->type() == QEvent::HoverLeave && watched == m_parent)
{
m_animation->setStartValue(1.0);
m_animation->setEndValue(0);
m_animation->start();
return false;
}
if(event->type() == QEvent::HoverMove)
{
if(m_timerId!=-1)
{
killTimer(m_timerId);
}
m_timerId = this->startTimer(1500);
m_animation->setStartValue(m_animation->currentValue());
m_animation->setEndValue(0.99);
m_animation->start();
}
return QObject::eventFilter(watched,event);
}
void HideShowListView::clickButton()
{
if(m_list->isHidden())
{
m_list->show();
m_button->setText(QStringLiteral("隐藏"));
}
else
{
m_list->hide();
m_button->setText(QStringLiteral("显示"));
}
}
void HideShowListView::timerEvent(QTimerEvent *event)
{
if(event->timerId() == m_timerId)
{
m_animation->setStartValue(1.0);
m_animation->setEndValue(0);
m_animation->start();
killTimer(m_timerId);
m_timerId=-1;
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/137393.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...