QT 播放器之列表隐藏

首先需要有一个按钮用来显示和隐藏列表

    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;
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼游戏开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值