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


相关推荐

  • 二叉树层次遍历算法——C/C++

    二叉树层次遍历算法——C/C++二叉树层序遍历1、算法思想用一个队列保存被访问的当前节点的左右孩子以实现层序遍历。在进行层次遍历的时候,设置一个队列结构,遍历从二叉树的根节点开始,首先将根节点指针入队列,然后从队头取出一个元素,每取一个元素,执行下面两个操作:访问该元素所指向的节点若该元素所指节点的左右孩子节点非空,则将该元素所指节点的左孩子指针和右孩子指针顺序入队。此过程不断进行,当队列为空时,二叉树的层次遍历结束…

    2022年6月5日
    30
  • solidworks常用快捷键命令大全_solidworks怎么设置快捷键

    solidworks常用快捷键命令大全_solidworks怎么设置快捷键为了更好的节约大家的设计时间,小编花了一番功夫为大家整理出了一份SolidWorks的快捷键大全,希望可以帮助到大家。

    2026年4月17日
    5
  • QFile源码学习笔记

    QFile源码学习笔记之前简单介绍了Qt读写文件Qt之读写文件http://blog.csdn.net/zhuyunfei/article/details/51249378这里记录下自己学习QFile的笔记。1.在Qt之读写文件中,在打开模式中指定未Append模式,发现如果文件不存在会自动创建新文件,在QFile的源码中找到了原因,在open函数的定义中都有如下语句if(mode&Append)mode

    2022年6月11日
    31
  • python实现——ASCII谢尔宾斯基地毯

    python实现——ASCII谢尔宾斯基地毯目录一、题目内容:二、思路:三、python代码:4、总结一、题目内容:二、思路:看到这个图案,肯定是自相似的,所以肯定用递归解决这个问题。但是具体怎样的呢?(1)这个其实就是二维矩阵,所以是要判断每个位置是不是要填给定字符,如果确定了每个位置填或不填,那不就解决问题了嘛;(2)用两个for循环不就遍历了所有的位置了么,所以这个也简单,现在的难点在于如何知道每个位置要填,所以用一个函数判断;(3)这个判断的函数需要三个参数,一个是边长数,另外两个是坐标。边界条件是n=1就返回T.

    2022年7月13日
    18
  • java保留n位小数输出的几种方法

    java保留n位小数输出的几种方法一、四舍五入吧并保留两位小数类似于c语言printf的输出printf():doublex=8.055;System.out.printf(“%.2f\n”,x);//8.06format():doublex=8.055;System.out.format(“%.2f\n”,x);//8.06format()方法将double型转换为String型再输出1…

    2022年7月8日
    115
  • 提案过稿率翻倍!3步用AIGC搞定插画包装,效果图美到哭!

    提案过稿率翻倍!3步用AIGC搞定插画包装,效果图美到哭!

    2026年3月15日
    2

发表回复

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

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