Skip to content

Commit

Permalink
fix: 优化动画
Browse files Browse the repository at this point in the history
  • Loading branch information
mhduiy committed Jul 12, 2023
1 parent f8b1083 commit e149d3a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/customComponents/MButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ MButton::MButton(const QString &text, QWidget *parent) : QPushButton(text, paren

void MButton::setCategory(Category category)
{
qDebug() << category;
this->category = category;
setProperty("style", QVariant(category));

Expand Down
50 changes: 22 additions & 28 deletions src/customComponents/MSidebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ void MSidebar::initUI() {
foldBtn->raise(); //置于顶层
this->layout()->setMargin(5);

connect(&animationTimer, &QTimer::timeout, this, &MSidebar::moveWidget);
// connect(foldBtn, &QPushButton::clicked, this, &MSidebar::foldPage);
m_animation = new QPropertyAnimation();
m_animation->setTargetObject(this);
m_animation->setEasingCurve(QEasingCurve::OutCubic);
m_animation->setDuration(800);
m_animation->setPropertyName("pos");
}

void MSidebar::addWidgetItem(const QString &text, const QIcon& icon) {
Expand All @@ -53,36 +56,27 @@ void MSidebar::addWidgetItem(const QString &text, const QIcon& icon) {
}

void MSidebar::foldPage() {
cutX = this->pos().x();
if(cutX == -1 * this->width() + foldBtn->width()) { //当前是折叠状态
tarX = 0; //设置为展开
cutPos = this->pos();
tarPos = cutPos;
if(cutPos.x() == -1 * this->width() + foldBtn->width()) { //当前是折叠状态
tarPos.setX(0); //设置为展开
}
else if(cutX == 0){ // 当前是展开状态
tarX = -1 * this->width() + foldBtn->width();
else if(cutPos.x() == 0){ // 当前是展开状态
tarPos.setX(-1 * this->width() + foldBtn->width());
}
else {
else { // 当前在动画中
return;
}
animationTimer.start(1);
}

void MSidebar::moveWidget() {
if(tarX == cutX) {
animationTimer.stop();
QIcon icon = foldBtn->icon();
QPixmap pixmap = icon.pixmap(64, 64);
QTransform transform;
transform.rotate(180);
pixmap = pixmap.transformed(transform);
foldBtn->setIcon(pixmap);
}
else if(cutX < tarX){
cutX += 1;
}
else {
cutX -= 1;
}
this->move(cutX, this->pos().y());
m_animation->setStartValue(cutPos);
m_animation->setEndValue(tarPos);
m_animation->start(); //开始动画
// 翻转图标
QIcon icon = foldBtn->icon();
QPixmap pixmap = icon.pixmap(64, 64);
QTransform transform;
transform.rotate(180);
pixmap = pixmap.transformed(transform);
foldBtn->setIcon(pixmap);
}

void MSidebar::showEvent(QShowEvent *event) {
Expand Down
8 changes: 5 additions & 3 deletions src/customComponents/MSidebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "MButton.h"
#include <QTimer>
#include <QMouseEvent>
#include <QPropertyAnimation>

class MSidebar : public QWidget{
Q_OBJECT
Expand All @@ -35,12 +36,13 @@ public slots:
QListWidget *listWidget = nullptr;
QPushButton *foldBtn = nullptr;
QTimer animationTimer;
void moveWidget();

int cutX = 0;
int tarX = 0;
QPoint cutPos;
QPoint tarPos;


bool isFirstShow = true;
QPropertyAnimation *m_animation; //动画对象指针
};

#endif //SIMPLYTOOLS_MSIDEBAR_H
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int main(int argc, char *argv[])

QLoggingCategory::defaultCategory()->setEnabled(QtDebugMsg, true); //允许输出调试信息

w.resize(600,400);
w.resize(600,450);

// 移动到中间位置
QDesktopWidget *desktop=QApplication::desktop();
Expand Down
2 changes: 0 additions & 2 deletions src/mainWidget/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ MainWindow::MainWindow(QWidget *parent)
this->stackedWidget->setCurrentIndex(index);
});

qDebug() << "show";

statusBar()->hide();
setCentralWidget(main);
this->centralWidget()->layout()->setMargin(10);
Expand Down

0 comments on commit e149d3a

Please sign in to comment.