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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JMeter100个线程竟然只模拟出1个并发

    JMeter100个线程竟然只模拟出1个并发

    2020年11月20日
    184
  • dropna无效_drop from

    dropna无效_drop from需要加等号如df22=df22.dropna(how=”any”)

    2022年9月15日
    0
  • AArch64教程第一章

    AArch64教程第一章AArch64教程第一章AArch64是一个新的64位模式,它是ARMv8架构下的一部分,它于2011年随着ARM发布。它被逐步部署于智能手机和服务器。所以我认为现在学习一点关于此架构的知识是比较好的。硬件目前,有ARMv6/ARMv7的单板电脑是比较容易获得的,其中最流行的一个选择是树莓派。相反,支持64位ARMv8模式的单板电脑就没有那么多了,但是它们最近也慢慢变得流行了起来。例如,Pine64,ODROID-C2,Dragonboard410c,等等。它们中的任何一种都可以做64位开发,

    2022年10月16日
    0
  • 启动马达接线实物图_星三角降压启动电路图实物接线图「建议收藏」

    启动马达接线实物图_星三角降压启动电路图实物接线图「建议收藏」星三角降压启动实物接线第一个交流接触器输出U1V1W1接在电机的U1V1W1上,第二个交流接触器输出U2V2W2上就好了,关于倒电机方向,第一个交流接触器输出U1V1倒换,第二个交流接触器输出U2V2也同时倒换就行了。原理告诉你:星线启动时KM1和KM3吸合,形成星型启动,这时电机每相绕组电压是210V左右,所以降压。一定时间后KM3释放,KM2吸合,这时电机每相绕组电压是38…

    2022年6月6日
    146
  • Redis 6_redis scan keys

    Redis 6_redis scan keysJedis是java开发的操作redis的工具包。

    2022年9月10日
    0
  • 什么是弱网测试?为什么要进行弱网测试?怎么进行弱网测试?「建议收藏」

    什么是弱网测试?由于处在移动互联网盛行的时代,网络形态除了有线连接外,还有2G/3G/4G/Wifi/5G等多种手机网络连接方式。首先额外补充一些5G的知识;2分钟了解什么是5G。在前不久结束的MWC2018上,5G成了全球的一个热门话题,而国内对5G的关注度也是异常地高。实际上,与2G、3G、4G相比,我国在5G方面的布局并不晚于其他国家;而且中国三大运营商在中国5…

    2022年4月8日
    69

发表回复

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

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