当前位置: 首页 > news >正文

QT-左右侧边栏动画

QT-左右侧边栏动画

  • 演示效果
  • 一、核心程序
  • 二、链接


演示效果

在这里插入图片描述

一、核心程序

#ifndef SLIDEPANEL_H
#define SLIDEPANEL_H#include <QWidget>
#include <QPropertyAnimation>
#include <QPushButton>
#include <QVBoxLayout>class SlidePanel : public QWidget
{Q_OBJECTQ_PROPERTY(int offset READ offset WRITE setOffset)public:enum Direction {Left,Right};explicit SlidePanel(Direction direction, QWidget *parent = nullptr);int offset() const;void setOffset(int offset);void addWidget(QWidget *widget);void toggle();void showPanel();void hidePanel();private:Direction m_direction;int m_offset;int m_panelWidth;bool m_isVisible;QWidget *m_contentWidget;QPropertyAnimation *m_animation;float fParentWidth = 0;void initUI();
};#endif // SLIDEPANEL_H
#include "slidepanel.h"
#include <QApplication>
#include <QDesktopWidget>SlidePanel::SlidePanel(Direction direction, QWidget *parent): QWidget(parent), m_direction(direction),m_offset(0),m_panelWidth(500),m_isVisible(false)
{// 设置窗口属性setWindowFlags(Qt::FramelessWindowHint | Qt::Widget);setAttribute(Qt::WA_TranslucentBackground);//setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);if (parent != nullptr){//parent->widfParentWidth = parent->x() + parent->width();}// 创建动画m_animation = new QPropertyAnimation(this, "offset", this);m_animation->setDuration(300); // 动画持续时间initUI();// 初始隐藏hidePanel();
}int SlidePanel::offset() const
{return m_offset;
}void SlidePanel::setOffset(int offset)
{m_offset = offset;int x = 0;if (m_direction == Left){x = -m_panelWidth + offset;}else if (m_direction == Right){//x = QApplication::desktop()->availableGeometry().width() -offset ;x = fParentWidth - offset;}move(x, y());
}void SlidePanel::addWidget(QWidget *widget)
{if (widget && m_contentWidget) {m_contentWidget->layout()->addWidget(widget);}
}void SlidePanel::toggle()
{if (m_isVisible) {hidePanel();} else {showPanel();}
}void SlidePanel::showPanel()
{m_animation->setStartValue(0);m_animation->setEndValue(m_panelWidth);m_animation->start();m_isVisible = true;show();
}void SlidePanel::hidePanel()
{m_animation->setStartValue(m_panelWidth);m_animation->setEndValue(0);m_animation->start();m_isVisible = false;
}void SlidePanel::initUI()
{// 设置面板大小setFixedWidth(m_panelWidth);setMinimumHeight(QApplication::desktop()->availableGeometry().height() * 1);// 创建主布局QVBoxLayout *mainLayout = new QVBoxLayout(this);mainLayout->setContentsMargins(0, 0, 0, 0);// 创建内容容器m_contentWidget = new QWidget(this);m_contentWidget->setStyleSheet("background-color: white; border-radius: 4px;");QVBoxLayout *contentLayout = new QVBoxLayout(m_contentWidget);// 添加关闭按钮QPushButton *closeBtn = new QPushButton("->", this);if (m_direction == Left){closeBtn->setText("<-");}closeBtn->setStyleSheet("background: none; border: none; font-size: 16px; color: #666;");connect(closeBtn, &QPushButton::clicked, this, &SlidePanel::hidePanel);// 顶部布局(包含关闭按钮)QHBoxLayout *topLayout = new QHBoxLayout();if (m_direction == Left){topLayout->addStretch();topLayout->addWidget(closeBtn);contentLayout->addLayout(topLayout);mainLayout->addWidget(m_contentWidget);}else{topLayout->addWidget(closeBtn);topLayout->addStretch();contentLayout->addLayout(topLayout);mainLayout->addWidget(m_contentWidget);}}

二、链接

https://download.csdn.net/download/u013083044/91732292

http://www.dtcms.com/a/344414.html

相关文章:

  • 动态代理保姆级别
  • 大模型微调:从理论到实践的全面指南
  • 数据结构与算法之数组篇leetcode704 - 二分查找
  • kubernetes中pod的管理及优化
  • Spring Boot 自动配置全流程深度解析
  • Linux虚拟机安装FTP
  • 「越短越合法」型滑动窗口
  • Seaborn数据可视化实战:Seaborn基础图表绘制入门
  • 分布式日志分析平台(ELFK 与 EFK)理论
  • 【机器学习深度学习】大模型分布式推理概述:从显存困境到高并发挑战的解决方案
  • 技术干货|使用Prometheus+Grafana监控Tomcat实例详解
  • [特殊字符] TTS格局重塑!B站推出Index-TTS,速度、音质、情感表达全维度领先
  • TTC协议(TTS即ORACLE DATA)协议分析
  • 代码随想录刷题Day40
  • week3-[二维数组]最大列
  • 文件系统层面的可用块数量可用空间和比例
  • 【Python-Day 40】告别内存溢出!Python 生成器 (Generator) 的原理与实战
  • 网络抓包介绍
  • Conmi的正确答案——Ubuntu24.04禁用任何休眠
  • CTF-RSA-openssl-pem格式的key
  • C++中不加{}导致的BUG
  • 笔记本怎么才能更快散热?
  • vsCode或Cursor 使用remote-ssh插件链接远程终端
  • Flask数据库迁移实战指南
  • Flask电影投票系统全解析
  • 近期https接口的联调小记
  • STM32——SPI通信+W25Q64
  • 一体化伺服电机在特种机器人(炉管爬行器)中的应用案例
  • ShimetaPi M4-R1 :OpenHarmony 开发板解析
  • Mysql事务特性及原理