前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >QT 播放器之列表隐藏

QT 播放器之列表隐藏

作者头像
全栈程序员站长
发布2022-09-05 10:08:56
7820
发布2022-09-05 10:08:56
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

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

代码语言:javascript
复制
    m_button = new QPushButton(QStringLiteral("隐藏"),parent);
    m_button->resize(35,35);

当点击按钮的时候隐藏或显示列表

代码语言:javascript
复制
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("显示"));
    }
}

列表显示的时候按钮位于列表左侧,隐藏的时候位于窗口右边

代码语言:javascript
复制
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);
}

为鼠标添加透明度动画

代码语言:javascript
复制
    QGraphicsOpacityEffect * effect = new QGraphicsOpacityEffect();
    m_button->setGraphicsEffect(effect);
    effect->setOpacity(0);

    m_animation = new QPropertyAnimation(effect,"opacity",this);
    m_animation->setDuration(100);

鼠标进入窗口的时候按钮需要显示,移动至窗口外面的时候需要隐藏

鼠标在窗口停止移动超过1.5秒,隐藏鼠标

鼠标移动的时候,显示鼠标

代码语言:javascript
复制
    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();
    }
代码语言:javascript
复制
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;
    }
}

完整代码:

代码语言:javascript
复制
#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
代码语言:javascript
复制
#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;
    }
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/137393.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年6月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档