QT 播放器之列表隐藏

QT 播放器之列表隐藏首先需要有一个按钮用来显示和隐藏列表m_button=newQPushButton(QStringLiteral(“隐藏”),parent);m_button->resize(35,35);当点击按钮的时候隐藏或显示列表connect(m_button,&QPushButton::clicked,this,&HideShowListVi…

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

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

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
上一篇 2022年6月2日 上午7:00
下一篇 2022年6月2日 上午7:00


相关推荐

  • php基础设计模式(注册树模式、工厂模式、单列模式)

    php基础设计模式(注册树模式、工厂模式、单列模式)

    2021年11月6日
    42
  • 软件测试流程及主要用例设计方法[通俗易懂]

    软件测试流程及主要用例设计方法[通俗易懂]软件测试流程及主要用例设计方法测试新手人门,首先要掌握测试的流程和实际运作项目流程和基础的用例设计方法。掌握测试和项目流程是了解研发过程中测试的主要工作;掌握最主要的用例设计方法就是掌握测试岗位最基本最核心的技能—如何测试。1.软件测试流程1.1测试流程测试流程:需求分析和讨论>编写测试计划>测试设计>测试执行>缺陷管理>测试报告。1)需求分析和讨论:分析…

    2022年5月15日
    41
  • RPC协议了解

    RPC协议了解1.RPC概述RPC(RemoteProcedureCallProtocol)远程过程调用协议。通俗的描述是:客户端在不知道调用细节的情况下,调用存在于远程计算机上的某个过程或函数,就像调用本地应用程序中的一样。正确的描述是:一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。2.RPC特点:2.1)RPC是协议:协议意味着规范。目前典型的RPC实现包括Dubbo、Thrift、Herrty等。但这些实现往往都会附加其他重要功能,例如Dubbo还包括服务管理、访问权限

    2022年5月19日
    65
  • pycharm使用qtdesigner

    pycharm使用qtdesigner1 第一步 pycharm 联合 anaconda 安装 2 安装 PyQt5 和 PyQt tools 在这里 PyQt tools 无法直接从 anaconda 环境中安装 且不加 user 会出现报错 pipinstallus tools ihttps pypi douban com simple 在 file 里的 setting 打开 externaltool 加工具添加 qtdesi

    2026年3月27日
    2
  • Spring整合mybatis完整示例

    Spring整合mybatis完整示例Spring整合mybatis完整示例  实现功能:根据id查找用户信息。 1、首先创建一个与表中数据相对应的实体类,User.javapackagebean;publicclassUser{intid;Stringname;intage;Stringsex;Stringschool;pu…

    2022年4月29日
    35
  • sqlserver事务锁死_sql触发器格式

    sqlserver事务锁死_sql触发器格式一、触发器触发器(trigger)是SQL server 提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表 事件相关的特殊的存储过程,它的执行不是由程序调用,也不是手工启动,而是由事件来触发,当对一个表 进行操作( insert,delete, update)时就会激活它执行。触发器经常用于加强数据的完整性约束和业务 规则等。 触发器可以从 DBA_TRIGGERS ,USER_TR…

    2022年8月18日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号