QT 播放器之列表隐藏
2023-09-14 09:05:26 时间
首先需要有一个按钮用来显示和隐藏列表
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);
}
为鼠标添加透明度动画
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QT嵌入式开发,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓
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;
}
}
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QT嵌入式开发,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓
相关文章
- Qt概述和Linux下安装
- C/C++ Qt StatusBar 底部状态栏应用
- Qt——Qt Creator编程软件常用的快捷键及Qt帮助文档的使用总结
- Linux系统使用Qt开发快速入门教程(linux下qt教程)
- Linux下开发Qt应用程序的技术指南(Linux和qt)
- 使用Qt快速设置Linux时间(qt设置linux时间)
- Linux安装Qt:一步一步指南(linux安装qt)
- Qt项目界面文件(.ui)及其作用(超详细)
- 使用Qt框架轻松连接MySQL数据库(qt与mysql连接)
- 在 Linux 上快速搭建 Qt 运行环境(qt运行环境linux)
- 开发Linux下 Qt开发:从入门到精通.(linux下的qt)
- 快速搭建 Qt 与 MySQL 的数据库应用(qt使用mysql)
- 简明易懂的Linux下QT安装指南(linux下安装qt教程)
- GUI应用Linux下Qt开发GUI应用:实现梦想的开发之路(linux下qt开发)
- 利用Qt轻松操作Oracle数据库(qt操作oracle)
- Linux系统中实现Qt串口通信的完美指南(qt串口通信linux)
- 使用Qt实现MySQL数据库连接教程(qt连接mysql数据库)
- Dooble是用Qt4写的另外一款WebKit浏览器,主要在保护用户隐私方面取胜。虽然它和其他浏览器提供的安全性相差不打,但是也有一些特有的属性比如内含的桌面等。Dooble桌面使得用户可以在增加桌面背景的同时添加应用程序启动,除了从软件包管理器安装外,还可以使用Ubuntu 10.10版本0.07安装,此时的Dooble版本为1.14,同时还要有Qt编码工具如qt4-qmake等。
- 使用Qt框架插入MySQL中的数据(qt插入mysql)
- 深入浅出:使用Qt连接MSSQL(qt 连接mssql)
- 轻松实现QT直连Microsoft SQL Server(qt直连mssql)
- Qt与SQLServer结合,提升开发效率(qt和sqlserver)
- 编程使用Qt快速实现SQLServer数据库编程(qt中sqlserver)
- 在Linux下体验Qt的简便安装之旅(在linux下安装qt)
- Linux下Qt音频播放:轻松享受音乐乐活力(linux下qt音频播放)
- MySQL与Qt的无缝连接(mysql 连接qt)