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