QT 播放器之列表隐藏
2023-06-13 09:11:56 时间
大家好,又见面了,我是你们的朋友全栈君。
首先需要有一个按钮用来显示和隐藏列表
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.cn/137393.html原文链接:https://javaforall.cn
相关文章
- Qt面试题(二)
- Linux开发_CentOS7.4服务器安装NFS、NGINX服务器,ffmpeg、Qt环境
- Qt官方示例-矢量变形
- Qt官方示例-标签对话框
- Linux系统使用Qt开发快速入门教程(linux下qt教程)
- Linux串口应用开发实践——Qt篇(linux串口qt)
- Qt学习之路详解编程语言
- 开源世界:Linux、Qt、OSG 联合构建未来(linuxqtosg)
- Linux虚拟机中安装Qt的指南(linux虚拟机安装qt)
- Linux下的Qt应用安装指南(qtlinux安装包)
- 开发Linux下用Qt开发:简单易用的开发方式(linux用qt)
- Linux系统下Qt环境搭建指南(linuxqt环境搭建)
- Adept Qt to Access Linux Commands(qt调用linux命令)
- QT快速加载MySQL驱动的简单操作(qt加载mysql驱动)
- 提高数据处理效率:探究Qt与MySQL数据库的结合应用(qt与mysql数据库)
- Linux QT支持中文交互体验(linux qt 中文)
- QT如何访问MySQL数据库(qt访问mysql数据库)
- 轻松实现QT直连Microsoft SQL Server(qt直连mssql)
- Qt与SQLServer结合,提升开发效率(qt和sqlserver)
- Qt技术与SQLServer的完美结合(qt与sqlserver)
- 界面让Linux拥有Qt完美图形界面的魅力(linux qt 图形)
- MySQL和QT不配套使用,无法实现事务处理(mysql不支持qt事务)